在TextBox中显示GridView编辑的行数据

时间:2015-10-30 04:10:34

标签: c# asp.net gridview

这是我的screenshot-output

当我单击编辑按钮,然后编辑的行数据需要显示在上面的文本框中(附带的屏幕截图)。

这是aspx文件:

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<center><div><h4>Student Details</h4></div></center>
    <table style="width: 100%;">

        <tr>
            <td>
                <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>

            </td>
            <td>
                &nbsp;
            </td>
            <td>
                <asp:TextBox ID="Textusername" runat="server"></asp:TextBox>

            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="Class"></asp:Label>

            </td>
            <td>
                &nbsp;
            </td>
            <td>
                <asp:TextBox ID="Textclass" runat="server"></asp:TextBox>

            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server" Text="Section"></asp:Label>

            </td>
            <td>
                &nbsp;
            </td>
            <td>
                <asp:TextBox ID="Textsection" runat="server"></asp:TextBox>
            </td>
        </tr>       

    <tr>
        <td>
            <asp:Label ID="Label5" runat="server" Text="Address"></asp:Label>
        </td>

        <td>
            &nbsp;
        </td>
        <td>
            <asp:TextBox ID="Textaddress" runat="server"></asp:TextBox>
        </td>
    </tr>
        <tr>
        <td>
            <asp:Button ID="btnsub" runat="server" Text="Submit" OnClick="btnsub_Click" OnClientClick="return register();" />
            <asp:Button ID="btnrst" runat="server" Text="Reset" OnClick="btnrst_Click" />

        </td>
        <td>
        </td>

        <td>

        </td>
    </tr>

</table>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="Column1,Column2,Column3,Column4" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
            <asp:BoundField DataField="Section" HeaderText="Section" 
                SortExpression="Section" />
            <asp:BoundField DataField="Address" HeaderText="Address" 
                SortExpression="Address" />
            <asp:ButtonField ButtonType="Button" CommandName="EditRow" HeaderText="Edit" 
                ShowHeader="True" Text="Edit" />
            <asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Delete" 
                ShowHeader="True" Text="Delete" />
        </Columns>
    </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="StoredProcedure2" 

        SelectCommandType="StoredProcedure">
            <DeleteParameters>
                <asp:Parameter Name="ID" />
                <asp:Parameter Name="Name" />
                <asp:Parameter Name="Class" />
                <asp:Parameter Name="Section" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="ID" />
                <asp:Parameter Name="Name" />
                <asp:Parameter Name="Class" />
                <asp:Parameter Name="Section" />
            </UpdateParameters>
    </asp:SqlDataSource>
</asp:Content>

任何人都可以帮助我,如何实现这一目标?

任何帮助都将受到高度赞赏,

感谢。,

2 个答案:

答案 0 :(得分:0)

要在Form中实现绑定值,您需要将TextBox值设置为GridView中的RowCommandEvent行值:

   protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
    {      
         if(e.CommandName =="EditRow")
         {
           GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
           string id = gr.Cells[0].Text;
           txtname.Text =gr.Cells[1].Text;
           txtclass.Text=gr.Cells[2].Text;
           txtsection.Text =gr.Cells[3].Text;
           txtaddress.Text=gr.Cells[4].Text; 
         }
    }

GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="Column1,Column2,Column3,Column4" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
            <asp:BoundField DataField="Section" HeaderText="Section" 
                SortExpression="Section" />
            <asp:BoundField DataField="Address" HeaderText="Address" 
                SortExpression="Address" />
            <asp:TemplateField HeaderText="Edit">
               <ItemTemplate>
                 <asp:Button runat="server" ID="btnedit" Text="Edit" CommandName="EditRow"></asp:Button>                    
               </ItemTemplate>
          </asp:TemplateField>
            <asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Delete" 
                ShowHeader="True" Text="Delete" />
        </Columns>
    </asp:GridView>

将行提取为:

protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
        {      
             if(e.CommandName =="EditRow")
             {
                 int index = Convert.ToInt32(e.CommandArgument);    
                 GridViewRow gr = GridView1.Rows[index];
                 string id = gr.Cells[0].Text;
               txtname.Text =gr.Cells[1].Text;
               txtclass.Text=gr.Cells[2].Text;
               txtsection.Text =gr.Cells[3].Text;
               txtaddress.Text=gr.Cells[4].Text; 
             }
        }

更多参考资料: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.commandname(v=vs.110).aspx

答案 1 :(得分:0)

首先,将GridView和显示文本框添加到更新面板

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
      <asp:UpdatePanel ID="updt" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False" >
                <ContentTemplate>
                 <center><div><h4>Student Details</h4></div></center>
        <table style="width: 100%;">

            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>

                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    <asp:TextBox ID="Textusername" runat="server"></asp:TextBox>

                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Class"></asp:Label>

                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    <asp:TextBox ID="Textclass" runat="server"></asp:TextBox>

                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Section"></asp:Label>

                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    <asp:TextBox ID="Textsection" runat="server"></asp:TextBox>
                </td>
            </tr>       

        <tr>
            <td>
                <asp:Label ID="Label5" runat="server" Text="Address"></asp:Label>
            </td>

            <td>
                &nbsp;
            </td>
            <td>
                <asp:TextBox ID="Textaddress" runat="server"></asp:TextBox>
            </td>
        </tr>
            <tr>
            <td>
                <asp:Button ID="btnsub" runat="server" Text="Submit" OnClick="btnsub_Click" OnClientClick="return register();" />
                <asp:Button ID="btnrst" runat="server" Text="Reset" OnClick="btnrst_Click" />

            </td>
            <td>
            </td>

            <td>

            </td>
        </tr>

    </table>
                 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="Column1,Column2,Column3,Column4" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
                <asp:BoundField DataField="Section" HeaderText="Section" 
                    SortExpression="Section" />
                <asp:BoundField DataField="Address" HeaderText="Address" 
                    SortExpression="Address" />
                <asp:ButtonField ButtonType="Button" CommandName="EditRow" HeaderText="Edit" 
                    ShowHeader="True" Text="Edit" />
                <asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Delete" 
                    ShowHeader="True" Text="Delete" />
            </Columns>
        </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="StoredProcedure2" 

            SelectCommandType="StoredProcedure">
                <DeleteParameters>
                    <asp:Parameter Name="ID" />
                    <asp:Parameter Name="Name" />
                    <asp:Parameter Name="Class" />
                    <asp:Parameter Name="Section" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="ID" />
                    <asp:Parameter Name="Name" />
                    <asp:Parameter Name="Class" />
                    <asp:Parameter Name="Section" />
                </UpdateParameters>
        </asp:SqlDataSource>
         </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
                </Triggers>       
            </asp:UpdatePanel>   
</asp:Content>

这是在RowCommandEvent

上获取编辑行的单元格值的方法
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditRow")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            string _name = row.Cells[1].Text;                
            string _class = row.Cells[2].Text;                
            string _section = row.Cells[3].Text;                
            string _Address = row.Cells[4].Text;                
            //Add this value to your text box here //
        }
    }