?
Solved

Label and Dropdownist in ASP.net

Posted on 2011-02-15
4
Medium Priority
?
319 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
3 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:
Rahul Agarwal earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 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