是否可以在UpdateCommand运行之前以编程方式在TextBox控件中输入值



(C#asp.net 3.5)我已成功在RowDataBound事件中预填充CheckBoxList3。在编辑模式下,用户可以进行其他checkbox选择。我已经成功地捕获了新值,创建了一个新的逗号分隔字符串,在单击更新链接后更新_RowUpdating事件中的SQL。问题是我的更新被GridView1s更新覆盖了*用户不会在TextBox2控件中实际输入新字符串

我似乎有两个选择:

  1. 传递根据checkboxlist3选择构建的逗号分隔字符串在运行UpdateCommand之前,以编程方式将控件设置为TextBox2。P*这可能吗*我谷歌上到处都找不到清晰的解决方案。我也在RowUpdating中尝试过这个代码,它起到了作用:

    TextBox tb2 = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox2"); tb2.Text = strCheckBoxList3.Substring(0, strCheckBoxList3.Length - 2);

  2. 手动更新sql,就像我只在"自然"更新之后放置sql调用一样(因为缺少更好的词语)。如果这是一个选项,中运行更新的方法,因为将其放在RowUpdating中总是会被颠倒。

HTML:

<asp:TemplateField HeaderText="Endorsements" SortExpression="Endorsements">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Endorsements") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Endorsements") %>'></asp:TextBox>
<asp:CheckBoxList ID="CheckBoxList3" runat="server" Font-Size="Small" RepeatDirection="Horizontal" onselectedindexchanged="CheckBoxList3_SelectedIndexChanged" 
AutoPostBack="True" >
<asp:ListItem Value="H">H</asp:ListItem>
<asp:ListItem Value="I">I</asp:ListItem>
<asp:ListItem Value="K">K</asp:ListItem>
<asp:ListItem Value="N">N</asp:ListItem>
<asp:ListItem Value="T">T</asp:ListItem>
<asp:ListItem Value="X">X</asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateField>

C#

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{             
//endorsements string
string strCheckBoxList3 = String.Empty;
//find endorsements checkboxlist in gridview. 
CheckBoxList cbl3 = (CheckBoxList)GridView1.Rows[e.RowIndex].Cells[1].FindControl("CheckBoxList3");
try
{
// Build Endorsements string
if (cbl3 != null)
{
// determine which checkboxes have been checked 
foreach (ListItem item in cbl3.Items)
{
// is item checked?
if (item.Selected == true)
{
// build string  
strCheckBoxList3 += (item.Value + ", ");
}//end of if
}// end of foreach
// Save the value in ViewState object before the PostBack
ViewState["vsEndorsementsString"] = strCheckBoxList3; 
}// end of if
}// end of endorsements try
catch (ArgumentOutOfRangeException ez)
{
System.Diagnostics.Debug.WriteLine(ez.Message + "; " + ez.Source + "; " + ez.TargetSite);
}
//Note: routine to update SQL was removed  here
}

// New: pass strings to sql Update Command Parameters for two checkboxlist columns in gridview
protected void sdsMySqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
string getViewStateEndorsementsString = ViewState["vsEndorsementsString"].ToString();
string getViewStateRestrictionsString = ViewState["vsRestrictionsString"].ToString();
foreach (System.Data.Common.DbParameter p in e.Command.Parameters)
{
if (p.ParameterName == "@Endorsements" && p.Value != null) 
{
//Assign @Endorsements parameter
e.Command.Parameters["@Endorsements"].Value = getViewStateEndorsementsString.ToString();
}//if
if (p.ParameterName == "@Restrictions" && p.Value != null) 
{
//Assign @Restrictions parameter
e.Command.Parameters["@Restrictions"].Value = getViewStateRestrictionsString.ToString();
}//if
} 
}

解决方案是将新值传递给SqlDataSource_Updateing事件中的Update Command Parameters。发布上面提供的更新代码。

我完全删除了在_RowUpdating事件中使用的SQL更新例程——这是不需要的。然后,我将新创建的逗号分隔字符串保存到ViewState对象中,并在SqlDataSource_Updateing事件中检索该对象。

我得出这个结论要归功于leoinlios,因为他在这里发表了一篇文章:在代码背后更改文本框值不会返回新值。另外,我读了很多关于View State的文章,发现这是最有用的文章:TRULY Understanding ViewState

最新更新