Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Label and Dropdownist in ASP.net

Posted on 2011-02-15
4
Medium Priority
?
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:
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 Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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