Gridview编辑模板 [英] Gridview Edit Template

查看:79
本文介绍了Gridview编辑模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有编辑模板的gridview.编辑模板包含一个下拉列表,当我创建gridview时,我将尝试填充该下拉列表.但是,我无法控制下拉列表.我在项目中的其他地方执行了相同的过程,并且工作正常,但是在这种情况下,它没有检索到下拉列表.请帮助:

我的网格代码:


I have a gridview with an edit template. The edit template contains a drop down list which i am trying to populate when the gridview is created. However, i am unable to get hold of the drop down list. I performed the same procedure somewhere else in the project and it works fine, but its not retrieved the drop down list in this case. Kindly help:

My Grid Code:


<asp:GridView ID="gvCallHistory" runat="server" AutoGenerateColumns="False"

                         CellPadding="3" Width="100%" Font-Names="Arial" Font-Size="14px"

                         DataKeyNames="LogID">
                         <RowStyle CssClass="ProfileRow" Wrap="False" />
                         <Columns>
                             <asp:TemplateField HeaderText="Time of Call">
                                 <EditItemTemplate>
                                     <asp:TextBox ID="txtCallTime" runat="server" BorderStyle="None" Height="25px"

                                         Text='<%# Bind("CallTime") %>'></asp:TextBox>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="Label1" runat="server" Text='<%# Bind("CallTime") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Comments">
                                 <EditItemTemplate>
                                     <asp:TextBox ID="txtComments" runat="server" BorderStyle="None" Height="63px"

                                         MaxLength="300" Text='<%# Bind("Comments") %>' TextMode="MultiLine"

                                         Width="218px" Font-Names="Arial" Font-Size="14px"></asp:TextBox>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="Label2" runat="server" Text='<%# Bind("Comments") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Attended By">
                                 <EditItemTemplate>
                                     <asp:DropDownList ID="ddlEditFaculty" runat="server" Font-Size="14px"

                                         SelectedValue='<%# Eval("FacultyID") %>' Width="150px">
                                     </asp:DropDownList>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="lblFname" runat="server" Text='<%# Bind("FName") %>'></asp:Label>
                                     <asp:Label ID="lblMName" runat="server" Text='<%# Bind("MName") %>'></asp:Label>
                                     <asp:Label ID="lblLName" runat="server" Text='<%# Bind("LName") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField HeaderText="Attended At">
                                 <EditItemTemplate>
                                     <asp:DropDownList ID="ddlEditLocation" runat="server" Font-Size="14px"

                                         SelectedValue='<%# Eval("LocationID") %>' Width="150px">
                                     </asp:DropDownList>
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:Label ID="Label3" runat="server" Text='<%# Bind("Location") %>'></asp:Label>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:TemplateField ShowHeader="False">
                                 <EditItemTemplate>
                                     <asp:CheckBox ID="chkIncoming" runat="server" Checked='<%# Eval("Incoming") %>'

                                         Font-Size="14px" Text="Incoming Call" />
                                 </EditItemTemplate>
                                 <ItemTemplate>
                                     <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"

                                         CommandName="Delete" Text="Delete"></asp:LinkButton>
                                 </ItemTemplate>
                             </asp:TemplateField>
                             <asp:CommandField ShowEditButton="True" />
                         </Columns>
                         <HeaderStyle CssClass="ProfileMiddle" HorizontalAlign="Left" />
                         <AlternatingRowStyle CssClass="ProfileAlternateRow" Wrap="False" />
                     </asp:GridView>





背后的代码:






Code Behind:


Protected Sub gvCallHistory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCallHistory.RowDataBound
    Try
        If e.Row.RowType = DataControlRowType.DataRow Then

            If gvCallHistory.DataKeys(e.Row.RowIndex).Values("Incoming") = True Then
                e.Row.ForeColor = Drawing.Color.Black
                e.Row.BackColor = Drawing.Color.FromArgb(51, 204, 51)
            ElseIf gvCallHistory.DataKeys(e.Row.RowIndex).Values("Incoming") = False Then
                e.Row.ForeColor = Drawing.Color.White
                e.Row.BackColor = Drawing.Color.FromArgb(230, 19, 19)
            End If


            Dim ddlFaculty As DropDownList = CType(e.Row.FindControl("ddlEditFaculty"), DropDownList)
            Dim ddlLocation As DropDownList = CType(e.Row.FindControl("ddlEditLocation"), DropDownList)

            Dim myUtil As New Utility
            myUtil.FillFaculty(ddlFaculty)
            myUtil.FillLocations(ddlLocation)

        End If

    Catch ex As Exception
    End Try
End Sub



当我尝试从行中获取ddlFaculty和ddlLocation时一无所获.



ddlFaculty and ddlLocation is nothing when i try to get it from the rows.

推荐答案

您可以在RowDataBound事件中的EditItemTemplate中获取控件.
You can get the controls within the EditItemTemplate in the RowDataBound event

Protected Sub gvCallHistory_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCallHistory.RowDataBound
Try
        If e.Row.RowType = DataControlRowType.DataRow Then
          If e.Row.RowState = DataControlRowState.Edit Then
                DropDownList ddl = GridView1.Rows          [e.RowIndex].FindControl("ddlEditFaculty") as DropDownList; 
          End If
        End If
Catch ex As Exception
End Try
End Sub



如果答案正确,则标记为答案.



If this is correct answer then Marked as Answer.


您可以添加一个合适的事件处理程序,当用户在编辑模式下更新数据时会触发该事件处理程序(Say,RowUpdating)并找到使用当前正在更新的相应行的RowIndex 进行控制.也就是说,类似于以下内容(我使用C#代码演示了此想法):

You can add a suitable event handler that is fired when user updates the data in the edit mode (Say, RowUpdating) and find the control using the RowIndex of the corresponding row that is currently being updated. That is, something llike the following (I used C# code to demonstrate the idea) :

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        DropDownList ddl = GridView1.Rows[e.RowIndex].FindControl("ddlEditFaculty") as DropDownList;
}


这篇关于Gridview编辑模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆