效果如下:
在选中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(); }