切换Gridview中的复选框
本文关键字:复选框 Gridview 切换 | 更新日期: 2023-09-27 17:49:39
我在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();
}
最后我知道该怎么做了。我创建了一个新的模板字段,并在该字段中保留了完整的未格式化的电话号码。我用代码隐藏了这个。我从不改变lblHiddenTel字段的值。
新模板字段 的标记<asp:TemplateField HeaderText="CopiedPhone" SortExpression="Telephone">
<EditItemTemplate>
<asp:TextBox ID="TextBox15" runat="server" Text='<%# Bind("Telephone") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblHiddenTel" runat="server" Text='<%# Bind("Telephone") %>' Visible="True"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
代码后面首先,隐藏数据行:
e.Row.Cells[19].Visible = false;
protected void chkTelephone_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
int CCnt = GridView1.HeaderRow.Cells.Count - 1;
CheckBox chkSwitch = GridView1.HeaderRow.Cells[CCnt].FindControl("chkToggle") as CheckBox; //true / false -- this has to be the number of columns.
Label lblTelephone = GridView1.Rows[i].FindControl("lblTelephone") as Label; //8885551234
Label lblHiddenTel = GridView1.Rows[i].FindControl("lblHiddenTel") as Label; //8885551234
string formatShort = chkSho.ToString() + "-" + lblHiddenTel.Text.Substring(lblHiddenTel.Text.Length - Math.Min(4, lblHiddenTel.Text.Length));
string formatFone = "(" + lblHiddenTel.Text.Substring(lblHiddenTel.Text.Length - 10, 3) + ") "
+ lblHiddenTel.Text.Substring(lblHiddenTel.Text.Length - 7, 3) + "-"
+ lblHiddenTel.Text.Substring(lblHiddenTel.Text.Length - Math.Min(4, lblHiddenTel.Text.Length));
if (chkSwitch.Checked)
{
lblTel.Text = formatShort;
}
else
{
lblTel.Text = formatFone;
}
}
}