在ASP.NET中的GridView上选择Button - CommandField

时间:2011-06-27 12:09:37

标签: c# asp.net gridview

我有以下代码在网页上创建GridView。我正在尝试禁用SelectButton或使其不可见,或者只是删除它。我试过

AutoGenerateSelectButton="false"

<asp:CommandField SelectText="Seç" Visible="false"
                                                                        ShowSelectButton="True"  />

我甚至删除了这部分

<Columns>
          <asp:CommandField          SelectText="Seç"                                                                            ShowSelectButton="True" />
 </Columns> 

它们都不起作用,SelectButton仍在那里。 我尝试使用

将其SelecText更改为
<asp:CommandField SelectText="Aç"                                                                            ShowSelectButton="True" />

这也行不通。我也试过了

ShowSelectButton="False" and it didn't change anything.

   <asp:UpdatePanel ID="UpdatePanelEnCokSatilanUrunler" runat="server">
             <ContentTemplate>
                      <asp:Panel ID="PanelEnCokSatilanUrunler" runat="server" 
                      GroupingText="En Çok Satılan Ürünler" 
                      BorderWidth="1" Font-Bold="true">
                           <table class="style1">
                               <tr>
                                  <td>
                                      <asp:GridView ID="GridView_EnCokSatilanUrunler" 
                                      runat="server"
                           OnRowDataBound="GridView_EnCokSatilanUrunler_RowDataBound"
                                       Font-Bold="false"
           OnSelectedIndexChanged="GridView_EnCokSatilanUrunler_SelectedIndexChanged"
                                       AllowSorting="true"
                                     OnSorting="GridView_EnCokSatilanUrunler_Sorting">
                                            <Columns>
                                                 <asp:CommandField SelectText="Seç"
                                                     ShowSelectButton="True"/>  
                                            </Columns>         
                                  </asp:GridView>
                     </td>
                    </tr>
                 </table>
               </asp:Panel>

              </ContentTemplate>

            </asp:UpdatePanel>

这就是我在aspx.cs中所拥有的,它对SelectButton

没有任何作用
protected void GridView_EnCokSatilanUrunler_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView_EnCokSatilanUrunler_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                ((LinkButton)e.Row.Cells[1].Controls[0]).Text = "Ürün No";
                ((LinkButton)e.Row.Cells[2].Controls[0]).Text = "Ürün Adı";
                ((LinkButton)e.Row.Cells[3].Controls[0]).Text = "Satış Miktarı";
                ((LinkButton)e.Row.Cells[4].Controls[0]).Text = "Ürün Durum";
                ((LinkButton)e.Row.Cells[5].Controls[0]).Text = "Ürün Tipi";
                ((LinkButton)e.Row.Cells[6].Controls[0]).Text = "Marka";
                ((LinkButton)e.Row.Cells[7].Controls[0]).Text = "Model";                
            }
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {

            }
        }

        protected void GridView_EnCokSatilanUrunler_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (EnCokSatilanUrunlerSortColumn == e.SortExpression)
            {
                if (EnCokSatilanUrunlerSortDirection)
                    EnCokSatilanUrunlerSortDirection = false;
                else if (!EnCokSatilanUrunlerSortDirection)
                    EnCokSatilanUrunlerSortDirection = true;
            }
            else
                EnCokSatilanUrunlerSortDirection = true;

            EnCokSatilanUrunlerSortColumn = e.SortExpression;

            EnCokSatilanUrunlerPageIndex = 0;

            GridView_EnCokSatilanUrunler.SelectedIndex = -1;


        }

        void EnCokSatilanUrunlerGridDoldur()
        {
            GridView_EnCokSatilanUrunler.DataSource = DAL.raporx.DAOUrunx.GetEnCokSatilanBesUrun(
                                                                                                    DateTime.Now - new TimeSpan(30, 0, 0, 0),
                                                                                                    DateTime.Now
                                                                                                );

            GridView_EnCokSatilanUrunler.DataBind();
        }

我只想要一个没有GridView的{​​{1}}。

3 个答案:

答案 0 :(得分:2)

删除某一行的选择:

RowDatBound中的

e.Row.Cells[0].Controls[0].Visible = false;

答案 1 :(得分:1)

只要您不需要SelectButton尝试将其从Columns部分移除,并从OnSelectedIndexChanged声明中删除GridView并删除GridView_EnCokSatilanUrunler_SelectedIndexChanged代码背后的方法。

答案 2 :(得分:1)

告诉Gridview不要渲染选择按钮

gv.AutoGenerateSelectButton = false;

接下来,添加模板字段。并在其中添加一个链接按钮。

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnSelect" Command="cmdSelect" Text="Select" runat="server"/>
</ItemTemplate>
</asp:TemplateField>

您需要gridview的RowDataBound事件的处理程序。您可以在标记或代码中执行此操作。我已经通过代码展示了如何做到这一点:

gv.RowDataBound += new EventHandler(OnRowDataBound);

事件接收器定义如下:

void OnRowDataBound(object sender, RowDataBoundEventArgs e)
{
    if(e.Row.RowType == RowType.Row)
    {
       LinkButton lbtnSelect = (LinkButton) e.Row.FindControl("lbtnSelect");
       //now hide or show as per you logic
    }
}

ps:匆忙编写代码,没有针对正确性。因此,部分代码可能不正确。如果您谷歌,您可能会找到上述正确的代码。我有空的时候会稍后编辑......