在ItemTemplate中编辑文本框后重新绑定GridView数据源

时间:2014-02-17 18:22:46

标签: c# asp.net gridview

我在页面加载时有一个带有数据源的DataGrid,但是当我对数据网格中的文本框进行更改时,我不知道如何获取这些新的textbox.text。

我不知道将新数据源更改为??

所以我的问题是如何更新数据源并保存我新编辑的TextBox.Text字段?? ...

    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;
        }

    }

我一直在寻找几个小时,发现没有什么真正与我的情况有关。我觉得应该有一个更好的方法来实现_RowEditing和_RowUpdating的所有事件处理程序,我已经尝试了这些但是他们的事件处理程序都没有被解雇....

编辑:继承我的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>

和SQLDataSource

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

0 个答案:

没有答案