开发者

Need help with customizing GridView control in Asp.Net

开发者 https://www.devze.com 2023-03-09 16:42 出处:网络
I build a customizable GridView with Templates and all that. I provided for <ItemTemplate> and <EditItemtemplate>fields having TextBoxes, DropDownList and RadioButtonList controls. Now i w

I build a customizable GridView with Templates and all that. I provided for <ItemTemplate> and <EditItemtemplate>fields having TextBoxes, DropDownList and RadioButtonList controls. Now i want to use Methods(functions) to present data(e.g, in the db table i have gender as True(forMale) and False(for Female).Now i want the Label control in GridView to call a method,pass entire GridView Row to it,then the method should return a string "Male" if gender==True in db and this string should be the text of the very label that called the method)....

Furthermore, when i EDIT any Row, I've placed say, RadioButtonList for gender,but it has no radiobutton selected ,by default.This leads to errors if user forgets to click a radio button. I want it to check the previous values for Gender and keep one of the radio Buttons selected depending on the previous values. Also, when i press the Edit button, i cant use FindControl method in GridView1_rowupdating method, to find the controls of <EditItemTemplate>. How can i find them?

Also, i have put two buttons in the GridView. I want these two buttons to be enabled for only those rows where approval_stage != zero.

Some of code is here:

MyFile.aspx:-

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True" PageSize="3" DataKeyNames="request_no" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" >

<Columns>

<asp:ButtonField DataTextField="request_no" HeaderText="request_no" CommandName="request_no" />

<asp:TemplateField HeaderText="date">

  <EditItemTemplate>
      <asp:Calendar ID="Calendar1" runat="server" ></asp:Calendar>
  </EditItemTemplate>

  <ItemTemplate>
      <asp:Label ID="Label5" runat="server" Text='<%# Bind("date") %>'></asp:Label>
  </ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="approval_stage">
  <EditItemTemplate>
    <asp:DropDownList ID="DropDownListApproval" runat="server">
      <asp:ListItem Value ="0" >0</asp:ListItem>
      <asp:ListItem Value ="1" >1</asp:ListItem>
      <asp:ListItem Value ="2" >2</asp:ListItem>
      <asp:ListItem Value ="3" >3</asp:ListItem>
      <asp:ListItem Value ="4" >4</asp:ListItem>
    </asp:DropDownList>
</EditItemTemplate>

<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# DisplayApproval(Eval("approval_stage")) %>'></asp:Label>
</ItemTemplate>



 </asp:TemplateField>
    <asp:BoundField DataField="name" HeaderText="name" />
    <asp:TemplateField HeaderText="gender">
    <EditItemTemplate>
    <asp:RadioButtonList ID="RadioButtonListGender" runat="server">
    <asp:ListItem Value="False">Male</asp:ListItem>
    <asp:ListItemValue="True">Female</asp:Li…
    </asp:RadioButtonList>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label2" runat="server" Text='<%# DisplayGender(Eval("gender")) %>'></asp:Label>
    </ItemTemplate>
    </asp:开发者_如何转开发TemplateField>
    <asp:ButtonField ButtonType="Button" CommandName="approve" Text="approve" />
    <asp:ButtonField ButtonType="Button" CommandName="reject" Text="reject" />
    <asp:CommandField ButtonType="Button" HeaderText="Edit" ShowEditButton="True" ShowHeader="True" />
    </Columns>
    </asp:GridView>



protected string DisplayGender(object gender)
{
string str = gender.ToString();
if (str == "False")
str = "Male";
else if (str == "True")
str = "Female";

return str;
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Some Code here
}


You can use findcontrol in the rowdatabound event of the gridview. See this: RowDataBound function of GridView

and some details on msdn: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

 protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
        {
            Label lbl = (Label)e.Row.FindControl("lblId");

        }

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消