我在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();
}
答案 0 :(得分:0)
我最终想出了如何做到这一点。我创建了一个新的模板字段,并在此字段中保留了完整的未格式化的电话号码。我用后面的代码隐藏了这个。我从不更改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;
}
}
}