gridview还是比较灵活的,有一种类似于excel的用法,可以直接在表格中进行添加,修改等操作。单元格数据比较段的表格用起来很方便。
可以参考https://blog.csdn.net/yingzhaom/article/details/50614599
我在这基础上改成了点击按钮弹出模态框进行操作。如有此类需求可以参考我的代码。
先从绑定数据源开始。
页面加载时绑定数据(可以把数据库连接封装,后面会多次使用): - //数据库连接
- SqlConnection sqlcon;
- SqlCommand sqlcom;
- string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
- protected void Page_Load(object sender, EventArgs e)
- {
-
- if (!IsPostBack)
- {
- //初始化table
- bind_gv();
- }
- }
- protected void bind_gv()
- {
- string sqlstr = "select * from 表";
- sqlcon = new SqlConnection(strCon);
- SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
- DataSet myds = new DataSet();
- sqlcon.Open();
- myda.Fill(myds, "表");
- gvTag.DataSource = myds;
- gvTag.DataKeyNames = new string[] { "tid" };
- gvTag.DataBind();
- sqlcon.Close();
- }
复制代码
前端代码(表中列的dataField与查询的列名对应): - <asp:GridView ID="gvTag" runat="server" AutoGenerateColumns="False" AllowSorting="True" CssClass="table table-striped" AllowPaging="True" OnPageIndexChanging="gvTag_PageIndexChanging" OnDataBound="gvTag_DataBound" PageSize="10" >
- <Columns>
- <asp:BoundField DataField="TID" HeaderText="Tag序号" ReadOnly="true" Visible="False" />
- <asp:BoundField DataField="GROUPCODE" HeaderText="Group" ReadOnly="true" ItemStyle-Width="70" />
- <asp:BoundField DataField="ELEMENTCODE" HeaderText="Element" ReadOnly="true" ItemStyle-Width="70" />
- <asp:BoundField DataField="TAGNAME" HeaderText="TagName" ReadOnly="true" ItemStyle-Width="260" />
- <asp:BoundField DataField="VID" HeaderText="VR序号" ReadOnly="true" Visible="False" />
- <asp:BoundField DataField="VRCode" HeaderText="VR" ReadOnly="true" ItemStyle-Width="50" />
- <asp:BoundField DataField="NOTES" HeaderText="描述" ReadOnly="true" />
- <asp:TemplateField HeaderText="操作" ItemStyle-Width="80" >
- <ItemTemplate>
-
- <asp:LinkButton ID="Modify" runat="server" CausesValidation="False" CommandArgument='<%# Eval("tid") %>' OnClick="Modify_Click" Text="编辑"></asp:LinkButton>
- <asp:LinkButton ID="Delete" runat="server" CausesValidation="False" CommandArgument='<%# Eval("tid") %>' OnClientClick="javascript:return confirm('确认要删除吗?')" AlternateText="删除此条目" OnCommand="Delete_Click" Text="删除"></asp:LinkButton>
- </ItemTemplate>
-
- </asp:TemplateField>
- </Columns>
- <PagerSettings Mode = "NextPreviousFirstLast" FirstPageText = "第一页" LastPageText = "末页" />
- <PagerTemplate>
- <table width="100%">
- <tr>
- <td width="75%">
- <asp:ImageButton ID="imgBtnFirst" runat="server" CommandArgument="First" CommandName="Page"
- ImageUrl="~/Images/First.png" ToolTip="第一页" />
- <asp:ImageButton ID="imgBtnPrev" runat="server" CommandArgument="Prev" CommandName="Page"
- ImageUrl="~/Images/Previous.png" ToolTip="上一页" />
- <asp:ImageButton ID="imgBtnNext" runat="server" CommandArgument="Next" CommandName="Page"
- ImageUrl="~/Images/Next.png" ToolTip="下一页" />
- <asp:ImageButton ID="imgBtnLast" runat="server" CommandArgument="Last" CommandName="Page"
- ImageUrl="~/Images/Last.png" ToolTip="最后页" />
- </td>
- <td align="right" width="25%">
- 页数:<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
- </td>
- </tr>
- </table>
- </PagerTemplate>
- </asp:GridView>
复制代码
分页后台代码: - protected void gvTag_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
- {
- gvTag.PageIndex = e.NewPageIndex;
- bind_gv();
- }
-
- protected void gvTag_DataBound(object sender, EventArgs e)
- {
- // 取得显示页数的那一列。
- GridViewRow pagerRow = gvTag.BottomPagerRow;
- if (pagerRow != null)
- {
- // 取得显示目前所在页数的 Label 控件。
- Label pageLabel = (Label)(pagerRow.Cells[0].FindControl("lblCurrentPage"));
- // 取得 第一页、上一页、下一页、最后页 的按钮。
- ImageButton imgBtnFirst =
- (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnFirst"));
- ImageButton imgBtnPrev =
- (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnPrev"));
- ImageButton imgBtnNext =
- (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnNext"));
- ImageButton imgBtnLast =
- (ImageButton)(pagerRow.Cells[0].FindControl("imgBtnLast"));
- // 设定何时应该显示 第一页、上一页、下一页、最后页 的按钮。
- if (gvTag.PageIndex == 0)
- {
- imgBtnFirst.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
- imgBtnPrev.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
- }
- else if (gvTag.PageIndex == gvTag.PageCount - 1)
- {
- imgBtnLast.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
- imgBtnNext.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
- }
- else if (gvTag.PageCount <= 0)
- {
- imgBtnFirst.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
- imgBtnPrev.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
- imgBtnNext.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
- imgBtnLast.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
- }
- if (pageLabel != null)
- {
- // 计算目前所在的页数。
- int currentPage = gvTag.PageIndex + 1;
- pageLabel.Text = currentPage.ToString() +
- " / " + gvTag.PageCount.ToString();
- }
- }
- }
复制代码
前台弹框代码: - <!--编辑-->
- <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
- <h4 class="modal-title" id="exampleModalLabel">编辑</h4>
- </div>
- <div class="modal-body">
- <asp:TextBox type="text" ID="TidEdit" runat="server" Visible="False"></asp:TextBox>
- <div class="form-group">
- <label for="TagEdit" class="control-label">Tag:</label>
- <asp:TextBox type="text" class="form-control" ID="TagEdit" runat="server" ReadOnly="True"></asp:TextBox>
-
- </div>
- <div class="form-group">
- <label for="TagNameEdit" class="control-label">TagName:</label>
- <asp:TextBox type="text" class="form-control" ID="TagNameEdit" runat="server"></asp:TextBox>
- </div>
- <div class="form-group">
- <label for="NotesEdit" class="control-label">描述:</label>
- <asp:TextBox type="text" class="form-control" ID="NotesEdit" runat="server" TextMode="MultiLine" Height="80px"></asp:TextBox>
- </div>
- <div class="form-group">
- <label for="ddlVREdit" class="control-label">VR:</label>
- <asp:DropDownList ID="ddlVREdit" runat="server" Height="34px"></asp:DropDownList>
- </div>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
- <asp:Button ID="SaveEdit" type="button" class="btn btn-primary" runat="server" Text="保存" OnClick="SaveEdit_Click" />
- </div>
- </div>
- </div>
- </div>
复制代码
后台弹框和编辑代码(里面的dao和impl是封装的数据库连接和更新的代码,TagInfo是封装的实体类): - //编辑弹框弹出
- protected void Modify_Click(object sender, EventArgs e)
- {
- LinkButton link = sender as LinkButton;
- int index = Convert.ToInt32(link.CommandArgument);
- TagInfoDao tag = new TagInfoDaoImpl();
- TagInfo tagInfo = tag.findByTid(index);
- //赋值
- TidEdit.Text = index.ToString();
- TagEdit.Text = tagInfo.groupCode +","+ tagInfo.elementCode;
- TagNameEdit.Text = tagInfo.tagName;
- NotesEdit.Text = tagInfo.notes;
- //弹出模态框
- ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
- scriptManager.RegisterStartupScript(typeof(string), "", "$('#editModal').modal('show');", true);
- //查询
- VRInfoDao vr = new VRInfoDaoImpl();
- List<VRInfo> vrList = vr.findVRInfo("");
- ddlVREdit.DataSource = vrList;
- ddlVREdit.DataTextField = "code";
- ddlVREdit.DataValueField = "VID";
- ddlVREdit.DataBind();
- ddlVREdit.SelectedIndex = tagInfo.vid - 1;
- }
- //编辑保存
- protected void SaveEdit_Click(object sender, EventArgs e)
- {
- TagInfo tag = new TagInfo();
- tag.tid = Convert.ToInt32(TidEdit.Text);
- tag.groupCode = TagEdit.Text.Split(',')[0];
- tag.elementCode = TagEdit.Text.Split(',')[1];
- tag.tagName = TagNameEdit.Text;
- tag.notes = NotesEdit.Text;
- tag.vid = Convert.ToInt32(ddlVREdit.SelectedValue);
- TagInfoDao tagDao = new TagInfoDaoImpl();
- if (tagDao.updata(tag))
- {
- Alert("修改成功!");
- }
- else
- {
- Alert("修改失败!");
- }
- bind_gv();
- }
复制代码
Alert弹框代码: - public void Alert(string str_Message)
- {
- ClientScriptManager scriptManager = ((Page)System.Web.HttpContext.Current.Handler).ClientScript;
- scriptManager.RegisterStartupScript(typeof(string), "", "alert('" + str_Message + "');", true);
- }
复制代码
删除后台代码: - //删除
- protected void Delete_Click(object sender, EventArgs e)
- {
-
- LinkButton link = sender as LinkButton;
- int index = Convert.ToInt32(link.CommandArgument);
- TagInfoDao tag = new TagInfoDaoImpl();
- if (tag.delete(index))
- {
- //删除成功
- Alert("删除成功");
- }
- else
- {
- //删除失败
- Alert("删除失败");
- }
- bind_gv();
- }
复制代码
新增跟编辑一样就不放代码了。 来源:https://blog.csdn.net/ab17171313/article/details/81504755 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |