Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在gridview上实现onclick行选择_C#_Asp.net - Fatal编程技术网

C# 如何在gridview上实现onclick行选择

C# 如何在gridview上实现onclick行选择,c#,asp.net,C#,Asp.net,有人能帮我吗?我不太会使用asp.net。我有一个充满数据的表,如果用户单击一行,所选行中的数据将填充输入字段。我已经使用网格视图中的onclick行选择创建了一个c#代码,它正在工作,但它只能选择一行,如果选择另一行,它不会选择它,并且它将保留在您首先单击的任何行上。如何使用onclick选择其他行 这是html gridview代码 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

有人能帮我吗?我不太会使用asp.net。我有一个充满数据的表,如果用户单击一行,所选行中的数据将填充输入字段。我已经使用网格视图中的onclick行选择创建了一个c#代码,它正在工作,但它只能选择一行,如果选择另一行,它不会选择它,并且它将保留在您首先单击的任何行上。如何使用onclick选择其他行

这是html gridview代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="CASE_KEY" DataSourceID="SqlDataSource1" Height="250px" 
    Width="1109px" BackColor="White" BorderColor="#999999" BorderStyle="None" 
    BorderWidth="1px" CellPadding="3" GridLines="Vertical" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
    OnRowDataBound="GridView1_RowDataBound">
    <AlternatingRowStyle BackColor="Gainsboro" />
    <Columns>
        <asp:BoundField DataField="CASE_KEY" HeaderText="CASE_KEY" ReadOnly="True" 
            SortExpression="CASE_KEY" Visible="False" />
        <asp:BoundField DataField="DEPARTMENT_CASE_NUMBER" 
            HeaderText="Department Case #" SortExpression="DEPARTMENT_CASE_NUMBER" />
        <asp:BoundField DataField="DEPARTMENT_NAME" HeaderText="Department" 
            SortExpression="DEPARTMENT_NAME" />
        <asp:BoundField DataField="CHARGE" HeaderText="Charge" 
            SortExpression="CHARGE" />
        <asp:BoundField DataField="LAB_CASE" HeaderText="Lab Case #" 
            SortExpression="LAB_CASE" />
        <asp:BoundField DataField="OFFENSE_DATE" HeaderText="Incident Report Date" 
            SortExpression="OFFENSE_DATE" />
    </Columns>

您的解决方案几乎是正确的,除了您应该使用GridView的
row命令
事件,而不是selectedIndexChange,然后您的解决方案应该可以工作

您可以陈述一些示例吗?。我对使用asp.net不太熟悉。提前感谢您以替代方式而不是
GridViewRow row=GridView1.SelectedRow
您可以使用
GridViewRow row=GridView1.Rows[GridView1.SelectedIndex]
获取当前选定的行。使用这种方法,您将不必使用row命令方式。
<table class="style2">
    <tr>
        <td class="style3">
            Department Case #</td>
        <td>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style3">
            Department</td>
        <td>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style3">
            Charge</td>
        <td>
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style3">
            Lab Case #</td>
        <td>
            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style3">
            Incident Report Date</td>
        <td>
            <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        </td>
    </tr>
</table>
 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Get the selected row
        GridViewRow row = GridView1.SelectedRow;
        if (row != null)
        {
            //Change the cell index(1) of column as per your design
            //Get the Selected row cell values here
            GridViewRow gr = GridView1.SelectedRow;
            TextBox1.Text = gr.Cells[1].Text;
            TextBox2.Text = gr.Cells[2].Text;
            TextBox3.Text = gr.Cells[3].Text;
            TextBox4.Text = gr.Cells[4].Text;
            TextBox5.Text = gr.Cells[5].Text;

        }
    }
    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Change the mouse cursor to Hand symbol to show the user the cell is selectable
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            //Attach the click event to each cells
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
        }
    }