2009-04-15 64 views
2

我有一個正在處理的Web應用程序(使用C#的ASP.NET 2.0)。其中,我有一個頁面上有超鏈接字段的GridView(My_Page.aspx)。當單擊超鏈接字段時,它會在同一頁面上顯示詳細信息。單擊超鏈接字段時獲取gridview的索引

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

我想知道如何在其中找到被點擊超鏈接的行的索引,因爲我想改變它的風格,讓用戶知道被點擊哪一行。

OR

我將如何改變它的風格,當用戶點擊超鏈接在GridView。

謝謝。

回答

1

在您的例子中,「指數」或者不如說是被點擊將在的Request.QueryString [「身份證」]

你可以從查詢字符串用的ID比較ID的超級鏈接的「身份證」您在RowDataBound事件中綁定的行。

或者,您可以在您的aspx中使用<%#DataBinder.Eval%>來根據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"); 
      } 
     } 
    } 
0

這是當選擇上選擇的節點的的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); 
} 

的GridView

<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> 

的DataSource

<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" /> 

Jav aScript

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