0

GridView を一括で編集および更新しようとしています。CheckBoxの最初の列にを生成しましたGridView。これは次のように機能します: で特定の行をチェックするとGridView、その行が編集可能になります。このように、編集したい行をいくつでもチェックできますGridView。すべての行を編集した後、UPDATE というユニバーサル ボタンを指定し、このボタンをクリックするとGridView、CheckBox.Check をチェックする各行でループが更新されます。

ここで直面している問題はCheckBoxGridView行のいずれかをクリックしても、 TextBox. LabelでをTextBoxチェックしてに変換しようとしCheckBoxてい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

この投稿を参照してください編集更新複数のレコードを削除

基本的に、グリッドビューに追加する必要があります

 <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 には存在しないはずです。TextBoxHome_Profileのテンプレートを記述し、可視性を 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 に答える