ASP.NET我没有从GridView RowUpdating事件的textbox字段获取值

时间:2016-09-02 05:20:52

标签: c# asp.net gridview

所以问题是我没有从GridView RowUpdating事件的textbox字段中获取值。

这是HTML ASP.net代码:

pwd

这里有代码:

<asp:GridView
    ID="GridView3"
    DataKeyNames="anPeriodID,acWorkerID"
    Visible ="true"
    runat="server"
    AutoGenerateColumns="false"
    ShowHeaderWhenEmpty="true" 
    OnRowUpdating="GridView3_RowUpdating"  
    OnRowEditing="GridView3_RowEditing"
    OnRowCancelingEdit="GridView3_RowCancelingEdit"         
    CssClass="display table table-striped table-bordered">
    <Columns>
        <asp:BoundField DataField="acPeriod" HeaderText="Obdobje" ItemStyle-Width="5%" ReadOnly="true" ItemStyle-Wrap="false" />
        <asp:BoundField DataField="acWorker" HeaderText="Zaposleni" ItemStyle-Width="5%" ReadOnly="true" ItemStyle-Wrap="false" />
        <asp:BoundField DataField="acReason" HeaderText="Vzrok za +/- 15 ur" ItemStyle-Width="85%" ControlStyle-Width="100%"/>
        <asp:TemplateField ShowHeader="False" ItemStyle-Width="5%" ItemStyle-Wrap="false">
            <ItemTemplate>
                <asp:LinkButton ID="Button_edit" runat="server" CausesValidation="false" CommandName="edit" CssClass="btn btn-xs btn-warning myconfirm" ToolTip="Uredi"><i class="ace-icon fa fa-pencil bigger-120"></i></asp:LinkButton>
                <%-- <asp:LinkButton ID="Button_delete" runat="server" CausesValidation="false" CommandName="delete" CssClass="btn btn-xs btn-danger" ToolTip="Izbriši"><i class="ace-icon fa fa-trash bigger-120"></i></asp:LinkButton> --%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update" CssClass="btn btn-xs btn-success" ToolTip="Potrdi"><i class="ace-icon fa fa-check bigger-120"></i></asp:LinkButton>
                <asp:LinkButton ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" CssClass="btn btn-xs btn-danger" ToolTip="Prekliči"><i class="ace-icon fa fa-reply bigger-120"></i></asp:LinkButton>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" SelectCommand="_upJN_GetPeriodData" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:SessionParameter Name="acUserID" SessionField="USERID" Type="String" />
        <asp:ControlParameter ControlID="CheckBox1" Name="all" PropertyName="Checked" />
        <asp:ControlParameter ControlID="CheckBox2" Name="curr" PropertyName="Checked" />
        <asp:Parameter Name="type" DbType="Int16" DefaultValue="2" /> 
    </SelectParameters>
</asp:SqlDataSource>

问题是我在这里假设:

protected void Grid_Reload(int i)
{
    DB db = new DB();

    if (i == 0 || i == 1)
    {
        SqlDataSource1.ConnectionString = db.GetConnString();
        GridView1.DataBind();
    }
    if (i == 0 || i == 2)
    {
        SqlDataSource2.ConnectionString = db.GetConnString();
        GridView2.DataBind();
    }
    if (i == 0 || i == 3)
    {
        SqlDataSource3.ConnectionString = db.GetConnString();
        GridView3.DataSource = SqlDataSource3;
        GridView3.DataBind();
    }    
}

protected void GridView3_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    String warning_msg = "", error_msg = "", info_msg = "", success_msg = "";
    int i = 0;
    Commons cm = new Commons();
    try
    {
        GridViewRow row = (GridViewRow)GridView3.Rows[e.RowIndex];
        int periodid = Convert.ToInt32(GridView3.DataKeys[e.RowIndex].Values[0]);
        string workerid = GridView3.DataKeys[e.RowIndex].Values[1].ToString();

        TextBox field1 = (TextBox)row.Cells[2].Controls[0];

        String SQL = "update _utJN_EvidencaDCSum set acReason = '" + field1.Text + "' where anPeriodID = '" + periodid.ToString() + "' and acWorkerID = '" + workerid + "'";

        DB db = new DB();
        db.ExecSQL(SQL);

        GridView3.EditIndex = -1;
        Grid_Reload(3);
    }
    catch (Exception ex)
    {
        error_msg = ex.Message;
    }
    finally
    {
        i = cm.SetMessage(LabelError, error, error_msg, LabelWarning, warning, warning_msg, LabelInfo, info, info_msg, LabelSuccess, success, success_msg);
    }

    if (i == 1)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#msg';", true);
    }
}

protected void GridView3_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView3.EditIndex = e.NewEditIndex;
    Grid_Reload(3);
}

protected void GridView3_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    Grid_Reload(3);
}

但我无法弄明白。 我有几乎相同的例子,但它的工作完美。

其他方法(一个正常工作):

TextBox field1 = (TextBox)row.Cells[2].Controls[0];

我也尝试了这个:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    String warning_msg = "", error_msg = "", info_msg = "", success_msg = "";
    int i = 0;
    Commons cm = new Commons();
    try
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);

        TextBox field1 = (TextBox)row.Cells[1].Controls[0];

        String SQL = "update _utJN_Holidays set acText = '" + field1.Text + "' where anID = '" + id.ToString()+ "'";

        DB db = new DB();
        db.ExecSQL(SQL);

        GridView1.EditIndex = -1;
        Grid_Reload();
    }
    catch (Exception ex)
    {
        error_msg = ex.Message;
    }
    finally
    {
        i = cm.SetMessage(LabelError, error, error_msg, LabelWarning, warning, warning_msg, LabelInfo, info, info_msg, LabelSuccess, success, success_msg);
    }

    if (i == 1)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#msg';", true);
    }
}

并且它仍然是空的..

2 个答案:

答案 0 :(得分:2)

你可以用另一种方式做到 TextBox textname = (TextBox)row.FindControl("your textbox id"); 然后在你想要代码的地方使用它的值

答案 1 :(得分:1)

好的,我明白了。问题基本上在Page_Load方法中,因为它总是重新加载Grid3 ..

正确的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["USERNAME"] == null)
    {
        Response.Redirect("~/Login.aspx");
    }

    if (!IsPostBack)
    {
        Grid_Reload(3);

    }

    Grid_Reload(1);
    Grid_Reload(2);
}