Solved

Label and Dropdownist in ASP.net

Posted on 2011-02-15
4
307 Views
Last Modified: 2013-11-08
Hello experts

I am trying to have a dropdownlist as an editable template and a label as a item template

I got the edititem template (dropdownlist) working to list days in a week, what I am trying to do now is to assign the dropdownlist value to the label once you choose it

I keep getting a null error reference
<asp:GridView ID="OvertimeDetailsGridView" 
                             OnRowDataBound="OvertimeDetailsGridView_OnRowDataBound" 
                             runat="server" 
                             DataSourceID="OverlistDetailsDataSource"
                             DataKeyNames="OvertimeID"             
                              AutoGenerateColumns="False" >
                                <Columns> 
                                    
                                    <asp:TemplateField HeaderText="Day" SortExpression="Day">            
                                        <EditItemTemplate>
                                            <asp:DropDownList  ID="DayDropDownList"   CssClass="gridViewText"  runat="server"> 
                                            </asp:DropDownList> 
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="DayLabel" Text="Test" runat="server" ></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField> 







 protected void OvertimeDetailsGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {


            if (e.Row.RowType == DataControlRowType.DataRow)
            {
              
                DropDownList ddl = new DropDownList(); 
                if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
                {
                    ddl = (DropDownList)e.Row.FindControl("DayDropDownList");
                    DateTime Monday = Convert.ToDateTime(weekComDropDownList.SelectedValue);
                    ddl.Items.Add(new ListItem("Monday " + Monday.ToShortDateString()));
                    ddl.Items.Add(new ListItem("Tuesday " + Monday.AddDays(1).ToShortDateString()));
                    ddl.Items.Add(new ListItem("Wednesday " + Monday.AddDays(2).ToShortDateString()));
                    ddl.Items.Add(new ListItem("Tuesday " + Monday.AddDays(3).ToShortDateString()));
                    ddl.Items.Add(new ListItem("Friday " + Monday.AddDays(4).ToShortDateString()));
                    ddl.Items.Add(new ListItem("Saturday " + Monday.AddDays(5).ToShortDateString()));
                    ddl.Items.Add(new ListItem("Sunday " + Monday.AddDays(6).ToShortDateString()));

                    Label DayLabel = (Label)OvertimeDetailsGridView.FindControl("DayLabel");
                    DayLabel.Text = ddl.SelectedItem.ToString();
                }

                
           

            }            
        }

Open in new window

0
Comment
Question by:samelamin
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34903262
I assume that you are getting the exception on this line:

    Label DayLabel = (Label)OvertimeDetailsGridView.FindControl("DayLabel");

The usual approach is to get the control from a GridViewRow, and not the top-level GridView.

Try this instead:

    Label DayLabel = (Label)e.Row.FindControl("DayLabel");
0
 
LVL 13

Accepted Solution

by:
agarwalrahul earned 500 total points
ID: 35056176
You Need to write the code on Dropdown_SelectedIndexChanged event because in gridview_rowdatabound event the value is not selected in Dropdown list, that's why its gives an error.


Try this Code:

<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" DataKeyNames="TourId"
    BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
    CellSpacing="2" Width="934px" Height="16px" ShowFooter="True"
    AutoGenerateColumns="False" onrowediting="GridView1_RowEditing"
    onrowcancelingedit="GridView1_RowCancelingEdit" >
   
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <Columns>    
     
      <asp:TemplateField HeaderText="BOOKING ID">
      <ItemTemplate>
      <asp:Label ID="lblBookingId" runat="server" Text='<%# Eval("BookingId") %>'></asp:Label>
      </ItemTemplate>
      <EditItemTemplate>
      <asp:Label ID="lblBookingId" runat="server" Text='<%# Eval("BookingId") %>'></asp:Label>
      </EditItemTemplate>
      </asp:TemplateField>
     
      <asp:TemplateField HeaderText="TOUR ID">
      <ItemTemplate>
      <asp:Label ID="lblTourId" runat="server" Text='<%# Eval("TourId") %>'></asp:Label>
      </ItemTemplate>
      <EditItemTemplate>
      <asp:TextBox ID="txtTourId" runat="server" Text='<%# Eval("TourId") %>' Width="50px"></asp:TextBox>      
      </EditItemTemplate>
      </asp:TemplateField>
             
      <asp:TemplateField HeaderText="TOUR NAME">
      <ItemTemplate>
     <asp:Label ID="lblTourname" runat="server" Text='<%# Eval("TourName") %>'></asp:Label>          
     
      </ItemTemplate>
      <EditItemTemplate>
      <asp:DropDownList DataSource='<%# GetTourName() %>'
      DataTextField="TourName" DataValueField="TourName" ID="ddlCategoryName"
      runat="server" Width="150px" AutoPostBack="True"      
                  OnSelectedIndexChanged="ddlCategoryName_SelectedIndexChanged">
      </asp:DropDownList>
     
      </EditItemTemplate>
      </asp:TemplateField>


Event code:

protected void ddlCategoryName_SelectedIndexChanged1(object sender, EventArgs e)
    {
    SqlConnection conn = new SqlConnection("data source=Lizzie;initial catalog=GlobalTravelCompany;user id=sa;password=sa");
    SqlCommand command = new SqlCommand("Select TourId From TourDetail Where TourName=@TourName", conn);
    SqlDataAdapter da = new SqlDataAdapter(command);
    da.SelectCommand.Parameters.Add("@TourName", SqlDbType.VarChar).Value = ((DropDownList)sender).SelectedValue;
    DataSet ds = new DataSet();
    da.Fill(ds);
    string a = ds.Tables[0].Rows[0]["TourId"].ToString();    
    TextBox txt = GridView1.Rows[GridView1.EditIndex].FindControl("txtTourId") as TextBox;
     if(txt != null)
          txt.Text = a;
   
    }

reference Link:
http://www.eggheadcafe.com/community/aspnet/2/10217116/assign-value-to-textbox-in-gridview-in-edititem-template.aspx
0
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 35906835
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now