Rebind a GridView Datasource after editing a Textbox in the ItemTemplate

非 Y 不嫁゛ 提交于 2019-12-12 06:33:33

问题


I have a DataGrid with a datasource on page load, but when I make changes to the Textboxes within the datagrid, I don't know how to get those new textbox.text.

I don't what to change the new datasource to??

So My question is how Do I update the datasource and save my new edited TextBox.Text field??...

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }

    }

    protected void BindData()
    {
        CustomerDao dao = new CustomerDao();
        currentCustomer = dao.SelectAll().First(x => x.CustomerID == 3) as Customer;

        //Assigning SQL Datasource to Gridview
        SqlDataSource1.SelectCommand = "SELECT CustomerID, FirstName, MiddleIntial, LastName, DateOfBirth, TaxID, GenderId FROM Customer " +
            "WHERE CustomerID = " + 3;
        ClientGridView.DataBind();
    }

     protected void btnSave_Click(object sender, EventArgs e)
    {
        //Set a new datasource here??

        string firstName = string.Empty;
        string middileInitial = string.Empty;
        string lastname = string.Empty;
        string date = string.Empty;
        DateTime dateOfBirth;
        string taxId = string.Empty;
        string strGender = string.Empty;
        int genderId = 0;


        foreach (GridViewRow r in ClientGridView.Rows)
        {
            firstName = ((TextBox)r.FindControl("txtFirstName")).Text;
            middileInitial = ((TextBox)r.FindControl("txtMiddleInitial")).Text;
            lastname = ((TextBox)r.FindControl("txtLastName")).Text;
            date = ((TextBox)r.FindControl("txtDateOfBirth")).Text;
            taxId = ((TextBox)r.FindControl("txtTaxID")).Text;
            strGender = ((TextBox)r.FindControl("txtGenderID")).Text;
        }

    }

I have been searching for hours and found nothing really relating to my situation. I also feel like there should be a much better way then to implement all the event handlers for _RowEditing and _RowUpdating and I have tried these but none of their event handlers fired anyway....

EDIT: Heres my GridView XAML

    <asp:GridView runat="server" ID="ClientGridView" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" OnRowEditing="ClientGridView_RowEditing" 
            Width="650px" PageSize="20" CssClass="gridView" DataSourceID="SqlDataSource1" OnDataBound="ClientGridView_DataBound" >
            <Columns>
                <asp:TemplateField HeaderText="First Name" HeaderStyle-CssClass="headerStyle">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="txtFirstName" Text='<%# Bind("FirstName") %>' CssClass="txtStyle"  />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Middle Initial" HeaderStyle-CssClass="headerStyle">
                    <ItemTemplate>
                        <asp:TextBox  runat="server" ID="txtMiddleInitial" Text='<%# Bind("MiddleIntial") %>' CssClass="txtStyle" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name" HeaderStyle-CssClass="headerStyle">
                    <ItemTemplate>
                        <asp:TextBox  runat="server" ID="txtLastName" Text='<%# Bind("LastName") %>' CssClass="txtStyle" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date of birth" HeaderStyle-CssClass="headerStyle">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="txtDateOfBirth" Text='<%# Bind("DateOfBirth") %>' CssClass="txtStyle" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Tax ID" HeaderStyle-CssClass="headerStyle">
                    <ItemTemplate>
                        <asp:TextBox  runat="server" ID="txtTaxID" Text='<%# Bind("TaxID") %>' CssClass="txtStyle" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Gender ID" HeaderStyle-CssClass="headerStyle">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="txtGenderID" Text='<%# Bind("GenderId") %>' CssClass="txtStyle" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

        </asp:GridView>

and the SQLDataSource

             <asp:SqlDataSource ID="SqlDataSource1" runat="server" CancelSelectOnNullParameter="false"
            ConnectionString=myconnectionString;User Id=userID;Password=pass; Persist Security Info=false;" />

来源:https://stackoverflow.com/questions/21836507/rebind-a-gridview-datasource-after-editing-a-textbox-in-the-itemtemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!