Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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
C# 动态添加的图像按钮未添加到page.Request.Form_C#_Asp.net - Fatal编程技术网

C# 动态添加的图像按钮未添加到page.Request.Form

C# 动态添加的图像按钮未添加到page.Request.Form,c#,asp.net,C#,Asp.net,我有一个带有固定ImageButton的表,一些ImageButton是在Page_Load中动态添加的。在“查看源”页面中,它们看起来相同,但ID不同: <tr> <td> <input type="image" name="ctl00$cphMain$ibSel_020" id="cphMain_ibSel_020" src="/Images/Deselected.gif" /> </td> <td>F

我有一个带有固定ImageButton的表,一些ImageButton是在Page_Load中动态添加的。在“查看源”页面中,它们看起来相同,但ID不同:

<tr>
   <td>
      <input type="image" name="ctl00$cphMain$ibSel_020" id="cphMain_ibSel_020" src="/Images/Deselected.gif" />
   </td>
   <td>Fixed ImageButton
   </td>
</tr>
<tr>
   <td>
      <input type="image" name="ctl00$cphMain$ibSel_001" id="cphMain_ibSel_001" src="/Images/Deselected.gif" />
   </td>
   <td>Dynamic ImageButton
   </td>
</tr>
....plus ten more dynamically added ImageButtons ibSel_002 through ibSel_011.
在Page_Load事件中,我通过遍历Page.Request.Form来确定哪个控件导致回发。如果我单击fixed ImageButton,则会在page.Request.Form中找到Ifsel_020。如果单击任何动态图像按钮,则找不到ID

我知道所有ImageButtons都会发生回发,因为我在页面中更新了回发。加载一个带有ID的标签,用于测试目的-对于所有dynamic ImageButtons,回发为空;对于fixed ImageButton,回发为ibSel 020

我如何判断是哪个动态添加的ImageButton导致了回发

下面是一些生成ImageButtons的代码:

for(int Q = 0; Q < g_zTypes.GetLength(0); Q++){
   TableRow tr = new TableRow();

   TableCell tc = new TableCell(); //Add image button to row

   ImageButton ib = new ImageButton();
   ib.ID = "ibSel_" + Q.ToString().PadLeft(3, '0');
   ib.ImageUrl = "/Images/Deselected.gif";
   tc.Controls.Add(ib);

   tr.Cells.Add(tc);

   tc = new TableCell(); //Add extra cell to row
   tc.Text = g_zTypes[Q, 1];
   tr.Cells.Add(tc);

   tblType.Rows.Add(tr);
}
希望能有帮助

更新:

为了进行测试,我编写了一段代码,将所有page.Request.Form元素累积到一个字符串中。在页面加载的最后,我是回应。写zMagic。这是一块:

//=============MAGIC CODE=========================
      Control cF;
      string zF;
      foreach(string zControl in Page.Request.Form){
         //For ImageButtons crop off mouse coordinates property
         if(zControl.EndsWith(".x") || zControl.EndsWith(".y")){
            cF = Page.FindControl(zControl.Substring(0, zControl.Length - 2));
         }
         else{
            cF = Page.FindControl(zControl);
         }
         zF = (cF == null) ? "NULL" : cF.ToString();
        zMagic += "<br />Form[" + zControl + "]=[" + zF + "]<br />";
      }
      zMagic += "[END]";
//================================================
我发现动态添加的ImageButtons实际上是存在的,不管我在页面加载的什么地方粘贴了“魔法代码”。发生的事情是Page.FindControl失败了——但前提是我在重新创建动态图像按钮的代码之前有“魔法代码”

底线:在重新创建Page.FindControlzDynamicControl之前,您无法创建它。

试试这个

这将帮助您获取导致回发的控件的id

在EventHandler中,您可以像这样获取控件的Id

void imgBtn_Click(object sender, ImageClickEventArgs e)
{
    //((ImageButton)sender).ID
}

如何生成和添加这些按钮?在创建按钮的位置添加代码buttons@Murali-Eh,呈现的HTML在我的原始帖子中。您是否尝试使用字符串senderPostBack=Request[_EVENTTARGET];?谢谢你的回复。该表实际上已经存在asp:table,正如存在fixed ImageButton的一个asp:TableRow一样。动态图像按钮被添加到新的asp:TableRow中的asp:Table中。实际上,我首先编码了您的解决方案,但由于鸡蛋问题,不得不放弃它。EH在页面加载后才会发生。但这太晚了,因为在此渲染中添加哪些动态图像按钮取决于单击了上一个渲染中的哪个动态图像按钮。我希望我说的很清楚,谢谢你的建议。