本文介绍了无法找到控件(DropDownlist)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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

推荐答案

DropDownList dd = this.Master.FindControl("DropDownList" + i) as DropDownList;





Update:

This is a simple project that works without any problem:



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;
        }
    }
}


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


这篇关于无法找到控件(DropDownlist)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 13:56