0

我试图批量编辑和更新我的 GridView。我CheckBox在我的GridView. 它的工作原理是这样的:如果我检查 中的特定行,则GridView该行可以编辑。像这样,我可以检查要编辑的行数GridView。我给出了一个名为 UPDATE 的通用按钮,在编辑完所有行后,单击此按钮,GridView更新循环在每一行中检查 CheckBox.Check。

我在这里面临的问题是,当我单击行CheckBox中的任何一个时GridView,我没有得到TextBox. 我正在尝试转换LabelTextBoxCheckBox.GridView

因此,当我检查一行时,Label根据我的程序,与该单元格的模板对应的文本不可见,但无法获得该TextBox单元格的值。

我试过的代码是这样的:

protected void OnCheckedChanged(object sender, EventArgs e)
    {
        bool isUpdateVisible = false;
        CheckBox chk = (sender as CheckBox);
        if (chk.ID == "chkAll")
        {
            foreach (GridViewRow row in GridView3.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
                }
            }
        }
        CheckBox chkAll = (GridView3.HeaderRow.FindControl("chkAll") as CheckBox);
        chkAll.Checked = true;
        foreach (GridViewRow row in GridView3.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
                for (int i = 3; i < row.Cells.Count; i++)
                {
                    row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
                    if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)//this condition is not satisfying when I debug the program. what is wrong in this line?
                    {
                        row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
                    }
                    if (isChecked && !isUpdateVisible)
                    {
                        isUpdateVisible = true;
                    }
                    if (!isChecked)
                    {
                        chkAll.Checked = false;
                    }
                }
            }
        }
        UpdateGrid.Visible = isUpdateVisible;
    }

aspx 代码是:

<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False"
DataKeyNames="Location_Profile_Name">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Location_Profile_Name" SortExpression="Location_Profile_Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Location_Profile_Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Home_Profile" SortExpression="Label10">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView>

我已经评论了我在上面的程序中面临的问题。请帮忙。

4

4 回答 4

1

我看到您正在混合使用方法、模板方法(ItemTemplate、EditTemplate)和一些代码隐藏方法(在代码后面进行硬编码)。对于您的场景,我建议在代码隐藏中执行此操作,以完全控制情况。我添加了您的代码来解释我的建议。这是:

<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Location_Profile_Name" SortExpression="Location_Profile_Name">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Location_Profile_Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Home_Profile" SortExpression="Label10">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:Label>
                <%--Here are the controls that edit this row, we will handle this on code-behind--%>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Home_Profile") %>' Visible="false"></asp:TextBox>
                <asp:Button ID="btnSave" Text="save" runat="server" Visible="false" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

“EditTemplate”已删除,您将处理自己的编辑模板。

        protected void OnCheckedChanged(object sender, EventArgs e)
    {
        //... Your code ...
        // Here we find the controls tha we will handle
        CheckBox chkAll = (GridView3.HeaderRow.FindControl("chkAll") as CheckBox);
        chkAll.Checked = true;
        foreach (GridViewRow row in GridView3.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox CheckBox1 = (CheckBox)row.FindControl("CheckBox1");
                Label Label2 = (Label)row.FindControl("Label2");
                TextBox TextBox1 = (TextBox)row.FindControl("TextBox1");
                Button btnSave = (Button)row.FindControl("btnSave");

                //GridView3.SetEditRow(row.RowIndex);
                if (CheckBox1 != null)
                {
                    if (CheckBox1.Checked)
                    {

                        if (TextBox1 != null && Label2 != null)
                        {
                            // Shows your "Edit Template"
                            btnSave.Visible = true;
                            Label2.Visible = false;
                            TextBox1.Visible = true;
                            TextBox1.Text = Label2.Text;
                        }
                    }

                }
            }
        }
    }

现在,您可以控制情况:)

于 2012-10-11T14:46:22.873 回答
0

看到这篇文章编辑更新删除多条记录

基本上你需要在你的gridview中添加

 <ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" 
              AutoPostBack="true" 
         OnCheckedChanged="chkSelect_CheckedChanged"/>
</ItemTemplate>

和后面的代码:

protected void chkSelect_CheckedChanged
                        (object sender, EventArgs e)
    {
        CheckBox chkTest = (CheckBox)sender;
        GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
        TextBox txtname = (TextBox)grdRow.FindControl("txtName");

        if (chkTest.Checked)
        {
            txtname.ReadOnly = false;                               
        }
        else
        {
            txtname.ReadOnly = true;                                
        }
    }

如果您熟悉 Jquery,您也可以轻松地在客户端启用这些字段

于 2012-10-11T14:15:12.443 回答
0

由于 AutoGenerateEditButton = false,因此 Home_Profile 应该没有。Home_Profile的TextBox模板应写入其中,可见性应设置为 false。

aspx 代码是:

<asp:TemplateField HeaderText="Home_Profile" SortExpression="Label10">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Home_Profile") %>'></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Home_Profile") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
于 2012-10-11T14:21:50.080 回答
-1

我没有我以前做的方式的代码,这里有一个小例子,使用与你所做的类似的逻辑,我希望它有所帮助

这是一个按照您的方式进行的工作示例

代码背后

public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");

        for (int i = 0; i < 10; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = "Test " + i;
            dt.Rows.Add(dr);
        }

        rptITem.DataSource = dt;
        rptITem.DataBind();
    }

}


protected void UpdateCB(object sender, EventArgs e)
{
    foreach (RepeaterItem Item in rptITem.Items)
    {
        CheckBox cb = (CheckBox)Item.FindControl("cbTest");
        TextBox tb = (TextBox)Item.FindControl("tbTest");
        Label lb = (Label)Item.FindControl("lbTest");
        tb.Visible = cb.Checked;
        lb.Visible = !cb.Checked;
    }
}

protected void UpdateAll(object sender, EventArgs e)
{
    foreach (RepeaterItem Item in rptITem.Items)
    {
        CheckBox cb = (CheckBox)Item.FindControl("cbTest");
        TextBox tb = (TextBox)Item.FindControl("tbTest");
        Label lb = (Label)Item.FindControl("lbTest");
        cb.Checked = true;
        tb.Visible = true;
        lb.Visible = false;
    }
} }

Aspx 代码

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Repeater ID="rptITem" runat="server">
      <HeaderTemplate>
      <div style="border:1px solid black">
        <div style="width:50px; float:left"><asp:CheckBox ID="cbAll" runat="server" AutoPostBack="true" OnCheckedChanged="UpdateAll" /></div>
        <div style="width:200px; float:left;">Name</div>
        <div style="clear:both"></div>
      </div>
      </HeaderTemplate>
      <ItemTemplate>

        <div style="width:50px; float:left"><asp:CheckBox ID="cbTest" runat="server" AutoPostBack="true" OnCheckedChanged="UpdateCB" /></div>
        <div style="width:200px; float:left;"><asp:Label ID="lbTest" runat="server" Text='<%# Eval("Name") %>' ></asp:Label></div>
        <div style="width:200px; float:left;"><asp:TextBox ID="tbTest" runat="server" Text='<%# Eval("Name") %>' Visible="false"></asp:TextBox></div>
      </ItemTemplate>
      <SeparatorTemplate>
        <div style="clear:both"></div>
      </SeparatorTemplate>
    </asp:Repeater>
</asp:Content>
于 2012-10-11T13:37:44.820 回答