Solved

Label and Dropdownist in ASP.net

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

776 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