【问题标题】:Getting index of a gridview when hyperlinkfield is clicked单击超链接字段时获取网格视图的索引
【发布时间】:2009-04-15 17:22:35
【问题描述】:

我正在开发一个 Web 应用程序(使用 C# 的 ASP.NET 2.0)。在其中,我有一个网格视图,页面上有一个超链接字段(My_Page.aspx)。单击超链接字段时,它会在同一页面上显示详细信息。

<asp:HyperLinkField DataNavigateUrlFields="ID" 
                    DataNavigateUrlFormatString="My_Page.aspx?id={0}"
                    DataTextField="NAME" 
                    HeaderText="Item1" 
                    SortExpression="NAME" />

我想知道如何找到点击超链接所在行的索引,因为我想改变它的样式,以便用户知道点击了哪一行。

当用户单击gridview 中的超链接时,我将如何更改它的样式。

谢谢。

【问题讨论】:

    标签: c# asp.net gridview coding-style detailsview


    【解决方案1】:

    在您的示例中,被点击的超链接的“索引”或更确切地说是“id”将在 Request.QueryString["id"]

    您可以将查询字符串中的 ID 与您在 RowDataBound 事件中绑定到的行的 ID 进行比较。

    或者,您可以在 aspx 中使用 根据 ID 字段和查询字符串设置样式。

    编辑:代码示例,尝试将其添加到您的代码后面。

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if(Request.QueryString["id"] != null &&
                    Request.QueryString["id"] == DataBinder.Eval(e.Row.DataItem, "id").ToString())
                {
                    e.Row.Style.Add("font-weight", "bold");
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      这是一个示例,当您在所选节点的 Gridview 子节点上选择一行时,它会显示在同一个 gridview 中:

      protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              LocationIDHiddenField.Value = Request.QueryString["LocationID"];
          }
          if (LocationIDHiddenField.Value != null && LocationIDHiddenField.Value != string.Empty)
              LoadLocationParents();
      }
      
      private void LoadLocationParents()
      {
          long locationID = Convert.ToInt64(LocationIDHiddenField.Value);
          bool IsCurrent = true;
          HyperLink parent;        
          Label seperator;
          do
          {
              Basic.Location.LocationProperties location = Basic.Location.LocationLoader.GetLocationProperties(locationID);
              parent = new HyperLink();
              seperator = new Label();
              if (!IsCurrent)  
                  parent.NavigateUrl = string.Format("LOCATIONLOV.aspx?LocationID={0}", location.LocationID);
              IsCurrent = false;
              parent.Text = location.LocationTitle;
              seperator.Text = "  >  ";
              ParentsPanel.Controls.AddAt(0, parent);
              ParentsPanel.Controls.AddAt(0, seperator);
              locationID = location.ParentID;     
          }
          while (locationID != 0);
          parent = new HyperLink();
          parent.NavigateUrl = "LOCATIONLOV.aspx";
          parent.Text = "upper nodes";
          ParentsPanel.Controls.AddAt(0, parent);
      }
      

      网格视图

      <asp:GridView ID="ChildsGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="LocationID"
                              DataSourceID="ChildsObjectDataSource" Width="570px" AllowPaging="True">
                              <Columns>
                                  <asp:TemplateField>
                                      <HeaderTemplate>
                                          &nbsp;
                                      </HeaderTemplate>
                                      <ItemStyle Width="20px" />
                                      <ItemTemplate>
                                          <a onclick="if ('<%# Eval("ChildCount") %>' == 'False') return false;" href='<%# Eval("LocationID", "LOCATIONLOV.aspx?LocationID={0}") %>' ><asp:Image  ID="GridLocationLov" runat="server" ToolTip="Expand" SkinID="LOVChilds" /></a> 
                                      </ItemTemplate>
                                  </asp:TemplateField>
                                  <asp:TemplateField HeaderText="Title" SortExpression="LocationTitleType">
                                      <ItemTemplate>
                                          <span class="LOVSelectText" onclick="LOCATIONID = '<%# Eval("LocationID") %>'; LOCATIONTITLE = <%= ConfirmTextBox.ClientID %>.value = '<%# Eval("LocationTitle") %>';ChangeSelectedRow(this);">
                                              <%# Eval("LocationTitleType")%>
                                          </span>
                                      </ItemTemplate>
                                      <HeaderTemplate>
                                          <asp:Label ID="GridHeadLabel" runat="server" OnLoad="GridHeadLabel_Load"></asp:Label>
                                      </HeaderTemplate>
                                  </asp:TemplateField>
                              </Columns>
                              <EmptyDataTemplate>
                               NO CHild
                              </EmptyDataTemplate>
                          </asp:GridView>
      

      数据源

      <asp:ObjectDataSource ID="ChildsObjectDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
          SelectMethod="Retrive" TypeName="BASIC.LOCATIONLOV.LOCATIONLOVLoader">
          <SelectParameters>
              <asp:ControlParameter ControlID="LocationIDHiddenField" Name="ParentID" PropertyName="Value"
                  Type="Int64" />
              <asp:Parameter DefaultValue="LocationTitle" Name="SortExpression" Type="String" />
          </SelectParameters>
      </asp:ObjectDataSource>
      <asp:HiddenField ID="LocationIDHiddenField" runat="server" />
      

      JavaScript

      function ChangeSelectedRow(sender)
      {
          if (SelectedRow != null)
              SelectedRow.style.backgroundColor = OriginalColor;
          SelectedRow = sender.parentElement.parentElement;
          OriginalColor = SelectedRow.style.backgroundColor;
          SelectedRow.style.backgroundColor = 'red';
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-08
        • 1970-01-01
        • 1970-01-01
        • 2015-04-28
        • 2011-08-31
        • 2018-06-26
        • 1970-01-01
        • 1970-01-01
        • 2018-01-02
        相关资源
        最近更新 更多