2011-06-01 67 views
0

我使用Templates和所有这些构建了一个可定制的GridView。我提供了<ItemTemplate><EditItemtemplate>具有TextBoxes,DropDownList和RadioButtonList控件的字段。现在我想使用方法(函数)来呈现数据(例如,在db表中,我将性别设置为True(forMale)和False(对于女性)。现在我希望GridView中的Label控件调用方法,传递整个GridView行,然后该方法应该返回一个字符串“男性”,如果性别==真在DB和这个字符串应该是文本的标签,称为该方法)...在Asp.Net中需要自定义GridView控件的帮助

此外,当我编辑任何Row,我已经说过,RadioButtonList是用于性别的,但默认情况下没有选择单选按钮。如果用户忘记单击单选按钮,则会导致错误。我希望它检查性别的以前的值,并根据以前的值保留一个单选按钮。 此外,当我按编辑按钮,我不能在GridView1_rowupdating方法中使用FindControl方法,找到<EditItemTemplate>的控件。我怎样才能找到他们?

此外,我已经把两个按钮在GridView中。我希望这两个按钮仅在Approval_stage!=零的那些行中启用。

一些代码是在这里:

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 
} 

回答

1

您可以在GridView的RowDataBound事件使用的FindControl。看到这一点:在MSDN上RowDataBound function of GridView

和一些细节: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

谢谢兄弟,但让我这样说:我如何才能找到的控制 ????从哪个方法? – suhail 2011-06-03 07:35:15

+0

在我的答案中添加了示例代码。 – FiveTools 2011-06-03 16:28:01