本文介绍了基于数据库的价值,我需要使GridView内部的按钮可见真/假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

值来自数据库,



如果值为pending,则按钮btnaccept visible = true,

如果值是接受然后按钮btnaccept visible = false,



但是从这段代码中所有的btnaccept按钮变得可见,是真的,



i需要它才是真的,只有当价值为待定时...请帮助



value is coming from database,

If the value is "pending" then button btnaccept visible = true,
If the value is "accept" then button btnaccept visible = false,

but from this code all the btnaccept button becoming visible true,

i need it to be true, only if the value is "pending" ... please help

void loadData()
    {
        DataTable dt = new DataTable();
        dt = comobj.GetAll_leave();
        grdData.DataSource = dt;
        grdData.DataBind();


        foreach (GridViewRow rw in grdData.Rows)
        {
            Button accept = (Button)rw.FindControl("btnaccept");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string ts = dt.Rows[i][8].ToString();

                if (ts == "pending")
                {
                    accept.Visible = true;
                }

                else
                {
                    accept.Visible = false;
                }

            }
        }
    }







<asp:GridView ID="grdData" runat="server" CssClass="grd" AutoGenerateColumns="False"
        EmptyDataText="No Record(s) found!" CellPadding="4" GridLines="Horizontal" AllowPaging="True"
        PageSize="15" BorderColor="Blue" OnPageIndexChanging="grdData_PageIndexChanging">
        <Columns>            
            <asp:TemplateField HeaderStyle-Width="5%" HeaderStyle-Wrap="false">
                <ItemTemplate>
                    <asp:Button ID="btnaccept" runat="server" CssClass="btn" Text="Accept" ForeColor="White"
                        BackColor="Green" OnClick="btnaccept_Click" />
                </ItemTemplate>
                <HeaderStyle Wrap="False" Width="5%"></HeaderStyle>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-Width="5%" HeaderStyle-Wrap="false">
                <ItemTemplate>                                         
                        <input id="btnreject" class="btn" type="button" value="Reject" style="color: white;
                            background-color: red" />                  
                   
                </ItemTemplate>
                <HeaderStyle Wrap="False" Width="5%"></HeaderStyle>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
        </Columns>
        <EmptyDataRowStyle CssClass="grd-emptydata" />
        <FooterStyle CssClass="grd-footer" />
        <HeaderStyle CssClass="grd-header" />
        <PagerSettings FirstPageImageUrl="~/images/icons/grid/first.png" LastPageImageUrl="~/images/icons/grid/last.png"
            Mode="NumericFirstLast" NextPageImageUrl="~/images/icons/grid/next.png" PageButtonCount="5"
            PreviousPageImageUrl="~/images/icons/grid/prev.png" />
        <PagerStyle CssClass="grd-pager" HorizontalAlign="Right" />
        <AlternatingRowStyle CssClass="grd-rowstyle-alt" />
        <RowStyle CssClass="grd-rowstyle" />
    </asp:GridView>

推荐答案

<Columns>

             <asp:BoundField HeaderText="Name" DataField="username">
                                                              <HeaderStyle HorizontalAlign="Left" />
                                                              <ItemStyle HorizontalAlign="Left" />
                                                          </asp:BoundField>
                                                          <asp:BoundField HeaderText="Status" DataField="status">
                                                              <HeaderStyle HorizontalAlign="Left" />
                                                              <ItemStyle HorizontalAlign="Left" />
                                                          </asp:BoundField>


          <asp:TemplateField HeaderStyle-Width="5%" HeaderStyle-Wrap="false">
              <ItemTemplate>
                  <asp:Button ID="btnaccept" runat="server" CssClass="btn" Text="Accept" ForeColor="White"
                      BackColor="Green"   />
              </ItemTemplate>
              <HeaderStyle Wrap="False" Width="5%"></HeaderStyle>
              <ItemStyle HorizontalAlign="Center" />
          </asp:TemplateField>


          <asp:TemplateField HeaderStyle-Width="5%" HeaderStyle-Wrap="false">
              <ItemTemplate>
                      <input id="btnreject" class="btn" type="button" value="Reject" style="color: white;
                          background-color: red" />

              </ItemTemplate>
              <HeaderStyle Wrap="False" Width="5%"></HeaderStyle>
              <ItemStyle HorizontalAlign="Center" />
          </asp:TemplateField>
      </Columns>










上面的
我将绑定字段名称和状态,我想你的地位是你的状态。








in above i will bound fields name and status,i think u have status coloumn in ur tabel.

protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
{
	if ((e.Row.RowType == DataControlRowType.DataRow)) {
		Button btn = e.Row.FindControl("btnaccept");
		string status = DataBinder.Eval(e.Row.DataItem, "status");
		if ((status == "Pending")) {
			btn.Visible = true;
		} else {
			btn.Visible = false;
		}
	}
}


这篇关于基于数据库的价值,我需要使GridView内部的按钮可见真/假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 11:33