Solved

Label and Dropdownist in ASP.net

Posted on 2011-02-15
4
312 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 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