繁体   English   中英

ASP.NET我没有从GridView RowUpdating事件的文本框字段中获取值

[英]ASP.NET I don't get a value from textbox field on GridView RowUpdating event

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

这是HTML ASP.net代码:

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

我也试过这个:

//Loop through all Cells in the Row.
int index = 0;
foreach (TableCell cell in row.Cells)
{
    if (cell.Controls.Count > 0)
    {
        //Check whether Cell has TextBox.
        if (cell.Controls[0] is TextBox)
        {
            //Reference the TextBox.
            TextBox textBox = cell.Controls[0] as TextBox;
            textBox.ID = "txtReason";
            //warning_msg += "xx" + GridView3.HeaderRow.Cells[index].Text + "xx" + index.ToString();
        }
    }
    index++;
}

//TextBox field1 = (TextBox)row.Cells[2].Controls[0];
TextBox field1 = (TextBox)row.FindControl("txtReason");

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

它仍然是空的..

您可以使用另一种方法,例如将TextBox textname = (TextBox)row.FindControl("your textbox id"); 然后在需要代码的地方使用它的值

好的,我知道了。 该问题基本上在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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM