在 Gridview 中切换复选框

Posted

技术标签:

【中文标题】在 Gridview 中切换复选框【英文标题】:Toggle Checkbox in Gridview 【发布时间】: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();

【问题讨论】:

【参考方案1】:

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


                           

【讨论】:

以上是关于在 Gridview 中切换复选框的主要内容,如果未能解决你的问题,请参考以下文章

在 Yii gridview 分页中保留复选框值

如何在gridview中绑定数据表中的复选框值

Gridview 中 TemplateField 中的复选框在回发时丢失

使用javascript在gridview中获取复选框选中行的相应行值

在gridview的末尾插入复选框列

使用 JQuery 在 GridView ASP.NET 中选择所有复选框