开发者

Selected row GridView won't extract data

I have a GridView which I am filling with data from my database. The data displays perfectly. My problem is that I want to be able to click on a row and then display the information that is in that row elsewhere (eventually I'll have a splitscreen to format the selected data nicely). The selection works,开发者_如何学运维 because it highlights the current row. However, I cannot figure out how to display that data elsewhere. For now, I'm trying to display the content of a single cell in a label called testLabel. I've tried many different methods, and the testLabel never updates when I click a new row. Can anybody see what is going wrong here?

C#

protected void DefaultGrid_SelectedIndexChanged(Object sender, EventArgs e)
{
    GridViewRow row = DefaultGrid.SelectedRow;
    testLabel.Text = row.Cells[2].Text;
}

ASPX

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID = "ScriptManager" runat="server" />
    <div>
        <div>
            <asp:Label runat="server" id = "orderByLabel" Text = "Order By: " />


            <asp:DropDownList runat="server" ID="orderByList" AutoPostBack="true">
                <asp:ListItem Value="fName" Selected="True">First Name</asp:ListItem>
                <asp:ListItem Value="lName">Last Name</asp:ListItem>
                <asp:ListItem Value="state">State</asp:ListItem>
                <asp:ListItem Value="zip">Zip Code</asp:ListItem>
                <asp:ListItem Value="cwaSource">Source</asp:ListItem>
                <asp:ListItem Value="cwaJoined">Date Joined</asp:ListItem>
            </asp:DropDownList>
        </div>
        <div>
            <asp:Label runat="server" ID="searchLabel" Text="Search For: " />
            <asp:TextBox ID="searchTextBox" runat="server" Columns="30" />
            <asp:Button ID="searchButton" runat="server" Text="Search" />
        </div>
    <div align="center">
    <asp:UpdatePanel ID = "up" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID = "orderByList"
        EventName="SelectedIndexChanged" />
         <asp:AsyncPostBackTrigger ControlId="searchButton" EventName="Click" />
    </Triggers>

    <ContentTemplate>
        <asp:GridView ID="DefaultGrid" runat = "server" DataKeyNames = "fName"
        onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
        autogenerateselectbutton = "true"
        selectedindex="0">
        <SelectedRowStyle BackColor="Azure"
        forecolor="Black"
        font-bold="true" />
        <Columns>
            <asp:ButtonField CommandName="Select" Visible="false" />
        </Columns>
        </asp:GridView>
        </ContentTemplate>
        </asp:UpdatePanel>



    </div>
    </div>
    <div>
        <asp:Label runat="server" ID="testLabel" Text="test" />
    </div>
    </form>
</body>
</html>


You have provided own select button and also set property http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogenerateselectbutton.aspx to true. You must choose only one of those.

Edit: Ah, see it now. Label 'testLabel' is outside updatepanel. Move it to ContentTemplate.


Have you tried placing testLabel inside another UpdatePanel and force an update on it after you change the label's text?


By the looks of it, it should be working. Here are a few pointers:

  • When accessing row.Cells[2].Text, keep in mind the index is Zero based (first cell is indexed 0).
  • You have a fixed column that is invisible. Although not showing, that column should count in your column indexing numbers.
  • Place a breakpoint inside DefaultGrid_SelectedIndexChanged. When it gets there, fool around with the possibilities by watching variables and calling methods. That should help you a lot.


I doubt your DefaultGrid_SelectedIndexChanged ever get called. Check this: http://forums.asp.net/t/1448445.aspx

Quote:

you need a buttonfield or a postback control such as a button, linkbutton,imagebutton with the commandName="Select" for the selectedIndexChanged event handler to trigger.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜