如何在更新面板中获取动态生成的控件(按钮)的ID

时间:2016-08-01 14:03:52

标签: c# asp.net updatepanel

Webform2.aspx

<div>
  <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
       <asp:UpdatePanel ID="UpdatePanel1" runat="server">
         <ContentTemplate>
              <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
          </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnQuestions" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>

            <asp:Button ID="btnQuestions" runat="server" Text="List of Questions" OnClick="btnQuestions_Click" />
        </div>

Webform2.aspx.cs

 protected void btnQuestions_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < 6; i++)
            {
                Button btn = new Button();
                btn.ID = i.ToString();
                btn.Text = "Goto Question No. - " + i.ToString();
                PlaceHolder1.Controls.Add(btn);
            }
        }

查看页面来源

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <form method="post" action="WebForm2.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJMzc1Mjk4OTk4ZGSudUK/VecpFioiNgMDP9kj5zbQVtZPCn6WnVBaEsjwEw==" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<script src="/WebResource.axd?d=Qdmym63UMpMU1DJtVFnuUSbvrfXRh_dBOIoxTsIpZRgeov32_hKWuqEbYXBgt2nVBPk_BfLrvvSwnb3dE41Ss01NOvbPIs0qaLVWdw91Esk1&amp;t=635145356180000000" type="text/javascript"></script>


<script src="/ScriptResource.axd?d=JwzAmVQVpITGxUtXHqHsNoS-RVwEUgrJeuMx8fcasEWLh6pC_B5U6DBCEmqJr6QsulrAlpuRr460-ujmC0zMiGfseMGkDsLZBsyJmuF2COzi1iL-SNRJHvkvxAhayUzHUP0eFCo09UvA7FrUPSsJv6AcuSY1gk8FbgpD08kqZYkPdKPGdgNK4k5EW51WZgFE0&amp;t=ca758f3" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
if (typeof(Sys) === 'undefined') throw new Error('ASP.NET Ajax client-side framework failed to load.');
//]]>
</script>

<script src="/ScriptResource.axd?d=aeRoBGSEi2hF_mWyw0oQ_Sh_Za3-vBRjUPJkdBiNvwjHO4rVkXySp2rInQ9ypkBTiuT6Al0xTxoHspgmG8H-vO74_u918eJQOOIapzFgxexsRMuD9zP6fC_NYvi-aEnSmX2MzrA4s4USb0ZwsyhN2T96dYVrUVfcu6AKtIkdGD_fsUvAvd0xXk9X_quQDz980&amp;t=ca758f3" type="text/javascript"></script>
<div class="aspNetHidden">

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAK/BowZL5VcjJwbsBqeLE4F+Xa+cAn637jOZCAF7/zPZrl/RNRCLQxrlWwoFLpNj5PHdoMHRAzcBvdD/CSQHZuG" />
</div>
    <div>        
        <script type="text/javascript">
//<![CDATA[
Sys.WebForms.PageRequestManager._initialize('ScriptManager1', 'form1', ['tUpdatePanel1','UpdatePanel1'], ['btnQuestions','btnQuestions'], [], 90, '');
//]]>
</script>

        <div id="UpdatePanel1">



</div>

        <input type="submit" name="btnQuestions" value="List of Questions" id="btnQuestions" />
    </div>
    </form>
</body>
</html>

问题 我想在动态生成的按钮控件上调用java-script函数。但是,在视图页面源中,UpdatePanel1下没有以下代码。

<input type="submit" name="1" value="Goto Question No. - 1" id="1" />
<input type="submit" name="2" value="Goto Question No. - 2" id="2" />
<input type="submit" name="3" value="Goto Question No. - 3" id="3" />
<input type="submit" name="4" value="Goto Question No. - 4" id="4" />
<input type="submit" name="5" value="Goto Question No. - 5" id="5" />

另外,我不想回发页面(因为其他功能受到影响)。喜欢

<asp:PostBackTrigger ControlID="btnQuestions" />

0 个答案:

没有答案