【发布时间】:2014-04-16 18:56:32
【问题描述】:
我在 Gridview 控件的 TemplateField 的 HeaderTemplate 中有一个复选框。我想使用此复选框将电话号码从完整的 10 位数字切换到最后 4 位数字,然后根据需要再次切换。当复选框不在 Gridview 中时,我对此没有任何问题。
我知道下面的代码不会工作,几天后我已经没有想法了,但除了使用 jQuery 之外,我似乎无法让我的大脑找到一种方法来做到这一点。
我在下面评论了我的代码并显示了我需要查看的值。任何想法都非常感谢。
TemplateField的HeaderTemplate:
<HeaderTemplate>
<asp:CheckBox ID="chkToggle" runat="server" AutoPostBack="True" EnableViewState="True" ViewStateMode="Enabled"
oncheckedchanged="chkTelephone_CheckedChanged" />
<asp:Label ID="lblTelephone" runat="server" Text="Telephone"></asp:Label>
</HeaderTemplate>
代码背后:
protected void chkTelephone_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkSwitch = GridView1.HeaderRow.Cells[i].FindControl("chkToggle") as CheckBox; //true / false
Label lblTelephone = GridView1.Rows[i].FindControl("lblTelephone") as Label; //8885551234 from checkbox in TemplateHeader
string result1 = lblTelephone.Text.Substring(lblTelephone.Text.Length - 10, 3); //888
string result2 = lblTelephone.Text.Substring(lblTelephone.Text.Length - 7, 3); //555
string result3 = lblTelephone.Text.Substring(lblTelephone.Text.Length - Math.Min(4, lblTelephone.Text.Length)); //1234
if (chkSwitch.Checked)
{
//Needs to show 1234
lblTelephone.Text = result3.ToString();
//once this line runs the value of lblTelephone.Text is now 1234 so when I Toggle again and chkSwitch is activated
//it now tries to peform the action on the new or current value of lblTelephone.Text which is set to 1234.
//This is my issue. I need to get it from the original 10 digit string
}
else
{
//needs to show 8885551234
lblTelephone.Text = result1.ToString() + result2.ToString() + result3.ToString();
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlCommand command = conn.CreateCommand();
command.CommandText = "TEST";
command.CommandType = CommandType.StoredProcedure;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
da.SelectCommand.Parameters.Add("@STUFF", SqlDbType.VarChar).Value = Stuff.Text;
da.SelectCommand.Parameters.Add("@MORESTUFF", SqlDbType.VarChar).Value = MoreStuff.Text;
da.Fill(ds);
GridView1.DataSourceID = "";
GridView1.DataSource = ds;
GridView1.DataBind();
}
【问题讨论】: