Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
<table class="auto-style1" id="table1"  runat="server" visible="false">
        <tr id="row1" visible="false">
            <td id="grade1"  runat="server" class="auto-style3"><asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="grade" DataValueField="grade"></asp:DropDownList></td>
            <td id="hrs1"  runat="server"><asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
        </tr>
        <tr id="row2" visible="false">
            <td id="grade2"  runat="server" class="auto-style3"><asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1" DataTextField="grade" DataValueField="grade"></asp:DropDownList></td>
            <td id="hrs2"  runat="server"><asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
        </tr>
        <tr id="row3" visible="false">
            <td id="grade3"  runat="server" class="auto-style3"><asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="SqlDataSource1" DataTextField="grade" DataValueField="grade"></asp:DropDownList></td>
            <td id="hrs3"  runat="server"><asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
        </tr>
        <tr id="row4" visible="false">
            <td id="grade4"  runat="server" class="auto-style3"><asp:DropDownList ID="DropDownList7" runat="server" DataSourceID="SqlDataSource1" DataTextField="grade" DataValueField="grade"></asp:DropDownList></td>
            <td id="hrs4"  runat="server"><asp:DropDownList ID="DropDownList8" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
        </tr>
        <tr id="row5" visible="false">
            <td id="grade5"  runat="server" class="auto-style3"><asp:DropDownList ID="DropDownList9" runat="server" DataSourceID="SqlDataSource1" DataTextField="grade" DataValueField="grade"></asp:DropDownList></td>
            <td id="hrs5"  runat="server"><asp:DropDownList ID="DropDownList10" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
        </tr>
        <tr id="row6" visible="false">
            <td id="grade6"  runat="server" class="auto-style3"><asp:DropDownList ID="DropDownList11" runat="server" DataSourceID="SqlDataSource1" DataTextField="grade" DataValueField="grade"></asp:DropDownList></td>
            <td id="hrs6"  runat="server"><asp:DropDownList ID="DropDownList12" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
        </tr>
        <tr id="row7" visible="false">
            <td id="grade7"  runat="server" class="auto-style3"><asp:DropDownList ID="DropDownList13" runat="server" DataSourceID="SqlDataSource1" DataTextField="grade" DataValueField="grade"></asp:DropDownList></td>
            <td id="hrs7"  runat="server"><asp:DropDownList ID="DropDownList14" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
        </tr>
        <tr id="row8" visible="false">
            <td id="grade8"  runat="server" class="auto-style3"><asp:DropDownList ID="DropDownList15" runat="server" DataSourceID="SqlDataSource1" DataTextField="grade" DataValueField="grade"></asp:DropDownList></td>
            <td id="hrs8"  runat="server"><asp:DropDownList ID="DropDownList16" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
        </tr>
        <tr id="row9" visible="false">
            <td id="grade9"  runat="server" class="auto-style3"><asp:DropDownList ID="DropDownList17" runat="server" DataSourceID="SqlDataSource1" DataTextField="grade" DataValueField="grade"></asp:DropDownList></td>
            <td id="hrs9"  runat="server"><asp:DropDownList ID="DropDownList18" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
        </tr>
        <tr id="row10" visible="false">
           <td id="grade10"  runat="server" class="auto-style3"><asp:DropDownList ID="DropDownList19" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
            <td id="hrs10"  runat="server"><asp:DropDownList ID="DropDownList20" runat="server" DataSourceID="SqlDataSource2" DataTextField="hours" DataValueField="hours"></asp:DropDownList></td>
        </tr>
        <tr id="submitrow"  runat="server">
           <td class="auto-style3"><asp:Button CssClass="submit" ID="Button2" runat="server" Text="Submit" OnClick="Button2_Click" /></td>
            
        </tr>
    </table>
 
 //to calculate the average of marks
        protected void Button2_Click(object sender, EventArgs e)
        {
            int num=Convert.ToInt32(Session["courses"]);
            for (int i = 1; i <=num ; i++)
            {
                DropDownList dd = (DropDownList)Page.FindControl("DropDownList" + i);
                string drpDLstVal = dd.SelectedValue.ToString();
                if (i % 2 == 0)
                {
                    hrs += Convert.ToDouble(drpDLstVal);
                }
                else
                {
                    grade += value(drpDLstVal);
                }
                
                
            }
            double avg = grade / hrs;
            Label1.Visible = true;
            Label1.Text = avg.ToString();
            
        }
 
        public double value(string grade)
        {
            switch (grade)
            {
                case "A":
                    mark = 4;
                    break;
 
                case "B+":
                    mark = 3.5;
                    break;
 
                case "B":
                    mark = 3;
                    break;
 
                case "C+":
                    mark = 2.5;
                    break;
 
                case "C":
                    mark = 2;
                    break;
 
                case "D+":
                    mark = 1.5;
                    break;
 
                case "D":
                    mark = 1;
                    break;
 
                case "F":
                    mark = .5;
                    break;
            }
            return mark;
        }
 
I can't get the drop down list to send it
Posted 12-May-13 6:05am
Comments
Nikfazan at 12-May-13 11:26am
   
No problem in code. It works fine.
Check
Convert.ToInt32(Session["courses"])
if it has the correct value.
amr mustafa at 12-May-13 11:29am
   
Checked it's ok...the value of num not null but...
string drpDLstVal = dd.SelectedValue.ToString(); is null
Note:this page in master page...is this a problem?
amr mustafa at 12-May-13 11:30am
   
Sorry this value is null:
DropDownList dd = (DropDownList)Page.FindControl("DropDownList" + i);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I hope this help:
    DropDownList dd = this.Master.FindControl("DropDownList" + i) as DropDownList;
 
Update:
This is a simple project that works without any problem:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication1.SiteMaster" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title></title>
</head>
<body>
    <form runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="1" Value="1" />
        <asp:ListItem Text="2" Value="2" />
        <asp:ListItem Text="3" Value="3" />
        <asp:ListItem Text="4" Value="4" />
    </asp:DropDownList>
    <asp:Button ID="Button1" runat="server" 
Text="Button" OnClick="Button1_Click1" />
    <asp:Label ID="Label1" runat="server" Text="MasterLabel"></asp:Label>
    <asp:ContentPlaceHolder ID="MainContent" runat="server">
    </asp:ContentPlaceHolder>
    </form>
</body>
</html>
 
using System;
using System.Web;
using System.Web.UI.WebControls;
 
namespace WebApplication1
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Button1_Click1(object sender, EventArgs e)
        {
            DropDownList dd = (DropDownList)FindControl("DropDownList"+1);
            string drpDLstVal = dd.SelectedValue.ToString();
 
            Label1.Visible = true;
            Label1.Text = drpDLstVal;
        }
    }
}
 

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
    <asp:Label ID="Label2" runat="server" Text="PageLabel"></asp:Label>
</asp:Content>
 
using System;
using System.Web;
using System.Web.UI.WebControls;
 
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Button1_Click1(object sender, EventArgs e)
        {
            DropDownList dd = (DropDownList)Master.FindControl("DropDownList"+1);
            string drpDLstVal = dd.SelectedValue.ToString();
 
            Label2.Visible = true;
            Label2.Text = drpDLstVal;
        }
    }
}
  Permalink  
v2
Comments
amr mustafa at 12-May-13 12:12pm
   
not work :(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

content = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
                if (content != null)
                {
                    dd = (DropDownList)content.FindControl("DropDownList" + i);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 12 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100