Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在同一页面上多次使用web用户控件?_Asp.net - Fatal编程技术网

Asp.net 如何在同一页面上多次使用web用户控件?

Asp.net 如何在同一页面上多次使用web用户控件?,asp.net,Asp.net,我已经为日历创建了web用户控件。也就是说,它有一个文本框和一个日历图像。单击日历弹出窗口,日历打开。然后,通过选择任何日期,它存储该日期的年份。我使用以下代码: <script language="javaScript" type="text/javascript" src="Scripts/Calendar.js"> </script> <link href="Styles/Calendar.css" rel="stylesheet" type="text/c

我已经为日历创建了web用户控件。也就是说,它有一个文本框和一个日历图像。单击日历弹出窗口,日历打开。然后,通过选择任何日期,它存储该日期的年份。我使用以下代码:

<script language="javaScript" type="text/javascript" src="Scripts/Calendar.js">  </script>
<link href="Styles/Calendar.css" rel="stylesheet" type="text/css" />
<table>
    <tr>
        <td>
            <input type="text" name="datum1"/>
        </td>
        <td>
            <img id="Img1" src="images/calFinal.jpg" alt="" runat="server" onclick="setYears(1947, 2040);
   showCalender(this, 'datum1');" />
        </td>
    </tr>
</table>
<div>
    <!-- Calender Script  -->
    <table id="calenderTable">
        <tbody id="calenderTableHead">
            <tr>
                <td colspan="4" align="center">
                    <select onchange="showCalenderBody(createCalender(document.getElementById('selectYear').value,
           this.selectedIndex, false));" id="selectMonth">
                        <option value="0">Jan</option>
                        <option value="1">Feb</option>
                        <option value="2">Mar</option>
                        <option value="3">Apr</option>
                        <option value="4">May</option>
                        <option value="5">Jun</option>
                        <option value="6">Jul</option>
                        <option value="7">Aug</option>
                        <option value="8">Sep</option>
                        <option value="9">Oct</option>
                        <option value="10">Nov</option>
                        <option value="11">Dec</option>
                    </select>
                </td>
                <td colspan="2" align="center">
                    <select onchange="showCalenderBody(createCalender(this.value, 
            document.getElementById('selectMonth').selectedIndex, false));" id="selectYear">
                    </select>
                </td>
                <td align="center">
                    <a href="#" onclick="closeCalender();"><font color="#003333" size="+1">X</font></a>
                </td>
            </tr>
        </tbody>
        <tbody id="calenderTableDays">
            <tr style="">
                <td>Sun</td>
                <td>Mon</td>
                <td>Tue</td>
                <td>Wed</td>
                <td>Thu</td>
                <td>Fri</td>
                <td>Sat</td>
            </tr>
        </tbody>
        <tbody id="calender">
        </tbody>
    </table>
    <!-- End Calender Script  -->
</div>

简
二月
破坏
四月
也许
六月
七月
八月
九月
十月
十一月
12月
太阳
周一
星期二
结婚
清华大学
星期五
坐
当我在同一web窗体上使用用户控件两次时,就会出现问题。我已经注册了控件。只要我用一次,它就能正常工作。但我想在两个地方买。因此,请给出一些解决方案,说明如何在同一web表单上多次使用用户控件。我为用户控件提供了不同的ID,但它仍然不起作用

用户控制代码:

<tr>
    <td class="directorytdWidth directorylabel">
        School Name:
    </td>
    <td class="directoryTdPadding">
        <asp:TextBox ID="txtSchoolName"  runat="server" Width="120px" ForeColor="#FF9F00"></asp:TextBox>
    </td>
</tr>
<tr>
    <td class="directorytdWidth directorylabel">
        School Passout Year:
    </td>
    <td class="directoryTdPadding">
        <uc1:calendar ID="schoolPassout" runat="server" />
    </td>
</tr>
<tr>
    <td class="directorytdWidth directorylabel">
        College Name:
    </td>
    <td class="directoryTdPadding">
        <asp:TextBox ID="txtCollegeName"  runat="server" Width="120px" ForeColor="#FF9F00"></asp:TextBox>
    </td>
</tr>
<tr>
    <td class="directorytdWidth directorylabel">
        College Passout Year:
    </td>
    <td class="directoryTdPadding">    
       <uc1:calendar ID="collegePassout" runat="server" />
    </td>
</tr>

学校名称:
学校休学年度:
学院名称:
大学毕业年份:

使用相同的用户控件,但ID不同。例如(参见下面的代码)

在网页中注册用户控件

 <%@ Register Src="~/Controls/SearchAffiliated.ascx" TagPrefix="uc" TagName="SearchAffiliated" %>

第一个用户控制呼叫

<uc:SearchAffiliated runat="server" ID="ucSearchAffiliated" />

第二个用户控制呼叫

<uc:SearchAffiliated runat="server" ID="SearchAffiliated1" />

请参见用户控件具有不同的ID,这适用于在一个web表单中调用相同的UC


干杯。

使用您希望usercontrol显示多少次的循环:

for(int i=0;i < n; i++) //Define the number of time that u want to display
{
   usercontrolname.id="UC"+i;//Assign the id for that usercontrol
   //add line of code,that controls in placeholder or panel
}
for(int i=0;i
您可以在同一页面中多次使用相同的用户控件。它可以是动态的,也可以是静态的。唯一的问题是我们需要为usercontrol指定不同的ID

这需要加入你的注册

<%@ Register Src="~/UserControls/Hello.ascx" TagPrefix="uc" TagName="Hello" %>

调用用户控件1

<uc:Hello runat="server" ID="ucHello1" />

调用用户控件2

<uc:Hello runat="server" ID="ucHello2" />

我也遇到了同样的问题,但问题与您喜欢在asp.net页面中使用的ID无关。因为我猜asp.net会自动为页面中没有ID的每个元素生成ID

问题是我复制了ascx文件,却忘了更改它们的类名。确保为每个用户控件使用不同的类名

<%@ Control Language="C#" ClassName="ucSchema2" %>

我知道我比赛迟到了,但也许有人可以用这个答案

在用户控件中为html元素使用静态客户端id,然后多次使用该用户控件将导致无效的html

您只允许在html页面上使用一次id,否则getElementById(返回单个元素)将不知道要获取哪个元素,并且可能无法获取任何元素

您需要使用户控件中的客户端ID真正唯一,即selectMonth、selectYear

这可以通过将用户控件的ClientID附加到元素id来实现。类似于这样的方式(不确定这种方法是否真的有效,但您已经了解了):



这样,客户端id应该是本地唯一id和页面唯一控件id的组合。

我已经尝试过了,即通过提供不同的id。但仍然不起作用。请给我一些其他解决方案。我没有收到任何错误,但当我选择任何日期时,它不会出现在文本框中,当我在一个页面上使用用户控件两次时。当我使用它一次时,它工作正常。我在一个问题中给出了用于此目的的java脚本,因此请检查它。并提出任何想法。您是否动态调用用户控件?或者给我你尝试过的确切代码…我不是动态调用用户控件。我给你的代码是学校毕业年份:
<select onchange="showCalenderBody(createCalender(document.getElementById('selectYear_<%=this.ClientID%>').value,
       this.selectedIndex, false));" id="selectMonth_<%=this.ClientID%>">