2

私が取り組んでいる 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" />

ハイパーリンクがクリックされた行のインデックスを見つける方法を知りたいのですが、そのスタイルを変更して、ユーザーがどの行がクリックされたかを知ることができるようにしたいからです。

また

ユーザーがグリッドビューでハイパーリンクをクリックしたときに、そのスタイルを変更するにはどうすればよいですか。

ありがとうございました。

4

2 に答える 2

1

あなたの例では、クリックされたハイパーリンクの「インデックス」またはむしろ「ID」は Request.QueryString["id"] になります

クエリ文字列の ID を、RowDataBound イベントでバインドしている行の ID と比較できます。

または、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");
            }
        }
    }
于 2009-04-15T17:26:51.757 に答える
0

選択したノードの 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';
}
于 2013-05-19T18:56:25.503 に答える