首页 新闻 搜索 专区 学院

GridView中的TemplateField 里嵌入CheckBox事件无法响应

0
悬赏园豆:20 [已解决问题] 解决于 2013-12-02 16:02

我在GridView中的TemplateField 里添加了CheckBox控件,可是这样没办法触发CheckBox1_CheckedChanged事件,在网上查了一下,有人说吧GridView的EnableViewState设为false才行,设为false后确实能够触发,但是这样会导致页面控件的一些值丢失,可是EnableViewState设为true的话,CheckBox无论怎么点击都是被选的状态,根本没法触发CheckedChanged事件,有什么好的办法吗?

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=FENG;Initial Catalog=shopping;Integrated Security=True"
DeleteCommand="DELETE FROM [shoppingcart] WHERE [goods_id] = @goods_id and [username]=@username"
InsertCommand="INSERT INTO [goods] ([information], [image], [price], [goods_id], [goods_name]) VALUES (@information, @image, @price, @goods_id, @goods_name)"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [information], [image], [price], [goods_id], [goods_name] FROM [goods] WHERE [goods_id] = @goods_id"


UpdateCommand="UPDATE [goods] SET [information] = @information, [image] = @image, [price] = @price, [goods_name] = @goods_name WHERE [goods_id] = @goods_id">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="goods_id" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="goods_id" Type="Int32" />
<asp:SessionParameter Name="username" SessionField="username" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="information" Type="String" />
<asp:Parameter Name="image" Type="String" />
<asp:Parameter Name="price" Type="Int32" />
<asp:Parameter Name="goods_name" Type="String" />
<asp:Parameter Name="goods_id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="information" Type="String" />
<asp:Parameter Name="image" Type="String" />
<asp:Parameter Name="price" Type="Int32" />
<asp:Parameter Name="goods_id" Type="Int32" />
<asp:Parameter Name="goods_name" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="goods_id" DataSourceID="SqlDataSource1" Height="229px"
Width="939px" onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="goods_id" HeaderText=""
SortExpression="goods_id" Visible="False" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:TemplateField HeaderText="">
<itemtemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Checked="True" oncheckedchanged="CheckBox1_CheckedChanged"/>
</itemtemplate>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:TemplateField>
<asp:ImageField DataImageUrlField="image" HeaderText="图片">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:ImageField>
<asp:BoundField DataField="information" HeaderText="商品"
SortExpression="information" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="price" HeaderText="单价" SortExpression="price" ItemStyle-BorderColor="Black">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="70px" />
</asp:BoundField>
<asp:TemplateField HeaderText="数量">
<itemtemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("amount")%>' Width="3em" CssClass="textbox"></asp:TextBox>
</itemtemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="70px" />
</asp:TemplateField>
<asp:CommandField HeaderText="操作" ShowDeleteButton="True" >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="70px" />
</asp:CommandField>
</Columns>
<AlternatingRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        
        CheckBox checkbox = (CheckBox)sender;
        if (checkbox.Checked)
        {
            total += Convert.ToInt32(GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text) * Convert.ToInt32(((TextBox)(GridView1.Rows[GridView1.SelectedIndex].Cells[5].FindControl("TextBox1"))).Text);
        }
        else
        {
            total -= Convert.ToInt32(GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text) * Convert.ToInt32(((TextBox)(GridView1.Rows[GridView1.SelectedIndex].Cells[5].FindControl("TextBox1"))).Text);
        }
        Label3.Text = "" + total.ToString() + ".00";

       

        
    }
sfgdjun的主页 sfgdjun | 初学一级 | 园豆:184
提问于:2013-12-01 14:24
< >
分享
最佳答案
0

<itemtemplate>
</itemtemplate>

这个有错误!

我测试了可以的,aspx代码如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:gridview ID="Gridview1" runat="server">
            <Columns>
               <asp:TemplateField ControlStyle-Height="20">
                   <ItemTemplate>
                        <asp:CheckBox ID="chk1" runat="server" oncheckedchanged="chk1_CheckedChanged" AutoPostBack="true"/>
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
            </Columns>
        </asp:gridview>
    </div>
    </form>
</body>
</html>

 

.cs 代码如下:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Gridview1.DataSource = new string[] { "1", "2", "3" };
                Gridview1.DataBind();
            }
        }

        protected void chk1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            if (chk != null)
            {
                ((Label)chk.Parent.FindControl("Label1")).Text = chk.Checked.ToString();
            }
        }

 

你试试应该能明白到你的代码错在哪里。

 

 

收获园豆:10
Albert Fei | 老鸟四级 |园豆:2102 | 2013-12-02 11:35
其他回答(1)
0

实在不好意思!我刚才试着用服务端控件,对服务端控件,实在不是很熟悉!而且感觉服务端控件,可控性不是特别好,如果你了解页面周期,会发现里面还是蛮复杂的,建议用html标签和javascript配合使用,不过让你一下子完全改变,要考虑项目,所以在asp:CheckBox 加上class,通过javascript获取到选中的checkbox,将值放到hidden这个是设置为runat="server",在后台获取hidden的值,就行了,不明白的话,给我发消息

收获园豆:10
秋壶冰月 | 园豆:5450 (大侠五级) | 2013-12-02 00:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册