How to finds control in gridview

Total Post:70

Points:494
C#  ASP.Net 
C#
 1229  View(s)
Ratings:
Rate this:

I have something weird happening, maybe i don't know of something?

I am trying to populate a Drop Down List (in the editItemTemplate) and also, when the Grid View Loads populate a column, with strings instead of the id's that it contains now.

<asp:TemplateField HeaderText="invsId" SortExpression="invsId">
 <EditItemTemplate>
      <asp:DropDownList ID="ddl_invNames" runat="server" AutoPostBack="True" />
 </EditItemTemplate>
 <ItemTemplate>
      <asp:Label ID="lbl_insLabel" runat="server" Text='<%# Bind("invsId") %>'></asp:Label>
 </ItemTemplate>
 <FooterTemplate>
      <asp:DropDownList ID="ddl_invNamesNew" runat="server" AutoPostBack="True" />
 </FooterTemplate>
</asp:TemplateField>

Cdebehind:

protected void gvAdminArticleAdd_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //111111
                //finding cotrols into the edit rows event              
                if (e.Row.RowState == DataControlRowState.Edit)
                {
                    DropDownList ddlImages = (DropDownList)e.Row.FindControl("ddlImages");
                    ddlImages.DataSource = GetPdfs();
                    ddlImages.DataBind();
 
                    DropDownList ddlinvsNames = (DropDownList)e.Row.FindControl("ddl_invNames");
                    ArrayList invList = GetInvestigatorNames();
                    ddlinvsNames.DataSource = invList;
                    ddlinvsNames.DataBind();
                }
                //222222
                //finding cotrols into rows
                Label insLabel = (Label)e.Row.FindControl("lbl_insLabel");
                int invsLabelId = int.Parse(insLabel.Text);
                insLabel.Text = connection.GetInvsNameById(invsLabelId);
 
            }
        }

The problem that I have occur in the RowDataBound event, I can't figure it up what is wrong

//111111 and //22222 are working correctly(if I comment one of them), but not together. how can it be?

if I place them together I am getting an error on this line of code

int invsLabelId = int.Parse(insLabel.Text);

  1. Post:194

    Points:1396
    Re: How to finds control in gridview

    //111111 and //22222 are working correctly(if I comment one of them), but not together. how can it be?
    You do not need those two to work together. Only one will be available at a given time.
    if (e.Row.RowState == DataControlRowState.Edit)
            {
               var ddlImages = (DropDownList)e.Row.FindControl("ddlImages");
               var ddlinvsNames = (DropDownList)e.Row.FindControl("ddl_invNames");
            }
            else
            {
               var insLabel = (Label)e.Row.FindControl("lbl_insLabel");
            }

      Modified On Apr-05-2018 06:39:26 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!