![](/img/trans.png)
[英]Retaining values of TextBox control I've programmatically created in C#
[英]Is it possible to programmatically input values in TextBox control before UpdateCommand runs
(C#asp.net 3.5)我已经在RowDataBound
事件中成功预填充了CheckBoxList3
。 在编辑模式下,用户然后可以选择其他checkbox
。 我已经成功捕获了新值,并创建了一个新的逗号分隔的字符串,该字符串在单击“更新”链接后更新了_RowUpdating
事件中的SQL。 问题是我的更新被GridView1s更新覆盖。 * 新字符串不是由用户实际输入到TextBox2控件中的。
看来我有两个选择:
在运行UpdateCommand之前,以编程方式将根据checkboxlist3选择生成的逗号分隔字符串传递给TextBox2控件。 P * 这可能吗? *我到处搜索Google,没有明确的解决方案。 我也在RowUpdating中尝试了这段代码,这有所作为:
TextBox tb2 = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox2"); tb2.Text = strCheckBoxList3.Substring(0, strCheckBoxList3.Length - 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 _Updating事件中的“更新命令参数”。 释放上面提供的更新代码。
我完全删除了_RowUpdating事件中具有的SQL更新例程-不需要。 然后,我将新创建的逗号分隔的字符串保存到ViewState对象,该对象在SqlDataSource _Updating事件中检索。
对我来说,归功于leoinlios是因为leoinlios在这里发表了他的文章: 在后面的代码中更改文本框的值不会回传新的值,此外,我阅读了很多有关View State的文章,发现这是最有用的文章: TRULY理解ViewState
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.