Gridview与DropDownList联动添加数据实时更新

 效果如下:

在选中ddl中的某项值后,GridView中呈现数据.

在tb中输入test.

点击新增按钮,数据直接呈现,并弹出提示对话框.该过程无页面刷新,用到了Ajax.

主要代码如下:

<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <table width="100%" style=" text-align:center; border-style: solid; border-width: 1px; border-collapse: collapse; font-size: 13px;" mce_style=" text-align:center; border-style: solid; border-width: 1px; border-collapse: collapse; font-size: 13px;"> <tr> <td> <table width="400" border="0" cellspacing="0" cellpadding="0" style="text-align:center; margin-top:5px;" mce_style="text-align:center; margin-top:5px;"> <tr> <td style="text-align: left; padding-left:5px; width: 160px;"> <asp:DropDownList ID="ddl" runat="server" AutoPostBack="True" onselectedindexchanged="ddl_SelectedIndexChanged" DataSourceID="SDSddl" DataTextField="CateName" DataValueField="CID"> </asp:DropDownList> </td> <td style="width:110px; padding-left:5px;"> <asp:TextBox ID="tbSearch" runat="server"></asp:TextBox> </td> <td style="text-align:left; padding-left:5px;" mce_style="text-align:left; padding-left:5px;"> <asp:Button ID="btnCheckMessage" runat="server" Width="65px" Text="新增" OnClientClick="return checkNull()" OnClick="btnCheckMessage_Click" /> </td> </tr> </table> </td> </tr> <div style="border-style: inset; border-width: 0px; height:295px; padding-top:5px;"> <yyc:SmartGridView ID="Sgv" runat="server" AllowPaging="false" AutoGenerateColumns="False" BoundRowClickCommandName="Select" CellPadding="3" DataKeyNames="CID" ForeColor="#333333" DataSourceID="sdsGVmessage"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <Columns> <asp:ButtonField CommandName="Select" Text="按钮" Visible="False" /> <asp:TemplateField HeaderText="序号"> <ItemTemplate> <%# Container.DataItemIndex + 1 %> </ItemTemplate> <HeaderStyle HorizontalAlign="Center" Width="50px" /> <ItemStyle HorizontalAlign="Center" Width="50px" /> </asp:TemplateField> <asp:BoundField DataField="CateName" HeaderText="考察知识点" ReadOnly="True" SortExpression="LoginName"> <HeaderStyle HorizontalAlign="Center" Width="100px" /> <ItemStyle HorizontalAlign="Center" Width="100px" /> </asp:BoundField> <asp:BoundField DataField="Ckid" HeaderText="考察内容序号" ReadOnly="True" SortExpression="LoginName"> <HeaderStyle HorizontalAlign="Center" Width="100px" /> <ItemStyle HorizontalAlign="Center" Width="100px" /> </asp:BoundField> <asp:BoundField DataField="CID" HeaderText="ID" ReadOnly="True" SortExpression="LoginName"> <HeaderStyle HorizontalAlign="Center" Width="20px" /> <ItemStyle HorizontalAlign="Center" Width="20px" /> </asp:BoundField> </Columns> <EmptyDataTemplate> <asp:Label ID="shownomessage" runat="server">暂无数据,请先新增科目或考点!</asp:Label> </EmptyDataTemplate> <AlternatingRowStyle BackColor="White" /> <RowStyle BackColor="#EFF3FB" /> </yyc:SmartGridView> </div> <asp:SqlDataSource ID="sdsGVmessage" runat="server" ConnectionString="<%$ ConnectionStrings:YaTouExamConnectionString %>" SelectCommand="select CID,Ckid,CateName FROM QuestionCate WHERE (Ckid = @ddlstring)"> <SelectParameters> <asp:Parameter Name="ddlstring" Type="String"/> </SelectParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SDSddl" runat="server" ConnectionString="<%$ ConnectionStrings:YaTouExamConnectionString %>" SelectCommand="SELECT * FROM [QuestionCate] where CkId=0"></asp:SqlDataSource>

后台代码:

protected void Page_Load(object sender, EventArgs e) { sdsGVmessage.SelectParameters["ddlstring"].DefaultValue = ddl.SelectedValue; if (!IsPostBack) { } } protected void btnCheckMessage_Click(object sender, EventArgs e) { string ddlvalue = ddl.SelectedValue; string sql = ""; //int i; string s; if (ddlvalue == "0") //增加考察内容 { sql = "insert into QuestionCate(CateName,CkId) values('" + tbSearch.Text.Trim() + "',0)"; } else { sql = "insert into QuestionCate(CateName,CkId) values('" + tbSearch.Text.Trim() + "','" + ddlvalue + "')"; } ExamDb.InsertOrUpdate(sql); ExamDb.AlertForPage(Page, "添加成功!"); Sgv.DataBind(); } protected void ddl_SelectedIndexChanged(object sender, EventArgs e) { string ddl1 = ddl.SelectedValue; string strsql = "Select * from QuestionCate where (CkId=" + ddl1 + ")"; sdsGVmessage.SelectCommand = strsql; Sgv.DataBind(); }

转载于:https://www.cnblogs.com/Renn/archive/2009/04/18/2048433.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值