在运行时创建命令 [英] Create command at runtime

查看:81
本文介绍了在运行时创建命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在DIV中创建一系列ImageButton



I must create a series of ImageButton inside a DIV

<div style="float:left; width:105px">
  <asp:ImageButton ID="ImageButton3" runat="server" ImageAlign="Middle" ImageUrl="~/Immagini/Arredi/thumbnails/File_-09.jpg" Width="100px" OnClick="ImageButton3_Click1"/>A1
</div>





HtmlGenericControl可以创建Html标签,但不能创建asp。 net





the HtmlGenericControl can create Html tags but not asp. net

System.Web.UI.HtmlControls.HtmlGenericControl dynDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
       dynDiv.ID = "dynDivCode";
       dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray");
       dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px");
       dynDiv.Style.Add(HtmlTextWriterStyle.Width, "300px");
       dynDiv.InnerHtml = "I was created using Code Behind";
       this.Controls.Add(dynDiv);





有办法创建ImageButton和属性吗?



我尝试了什么:



Div我可以创建它





there is a way to create ImageButton and Properties ?

What I have tried:

The Div I can create it

System.Web.UI.HtmlControls.HtmlGenericControl dynDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");





但Div内的ImageButton?



but the ImageButton inside the Div ?

推荐答案

<asp:Panel style="float:left; width:105px" runat="server" ID="myPanel">
    <asp:ImageButton ID="ImageButton3" runat="server" ImageAlign="Middle" ImageUrl="~/Immagini/Arredi/thumbnails/File_-09.jpg" Width="100px" OnClick="ImageButton3_Click"/>A1
</asp:Panel>







protected void Page_Load(object sender, EventArgs e)
{
        ImageButton newButton = new ImageButton();

        newButton.ID = "DynamicImageButton" + i.ToString();
        newButton.ImageAlign = ImageAlign.Middle;
        newButton.ImageUrl = "~/Immagini/Arredi/thumbnails/File_-09.jpg"; // change as needed
        newButton.Width = 100;
        newButton.Click += NewButton_Click;
        newButton.CommandArgument = i.ToString();

        myPanel.Controls.Add(newButton);

        Literal newLiteral = new Literal();

        newLiteral.Text = "Button " + i.ToString();

        myPanel.Controls.Add(newLiteral);
}

private void NewButton_Click(object sender, ImageClickEventArgs e)
{
    ImageButton b = (ImageButton)sender;

    System.Diagnostics.Debug.WriteLine("Button " + b.CommandArgument + " was clicked");
}


我使用带有ImageButton的DataList解决了



I Solved using a DataList with ImageButton

<!-- DataList Images -->
        <div style="overflow-x:scroll; overflow-y:no-content; height:280px"
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
              <ContentTemplate>
                  <!-- RepeatColumns="0" DataList Horizontal -->
                 <asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table" RepeatColumns="0">
                          
                   <ItemTemplate>
                    <td>                       
                       <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Container.DataItem %>' Height="200px" OnClick="ImageButton1_Click" ImageAlign="Middle" EnableTheming="False" CssClass="CCSImageButton"/>
                    </td>
                    </ItemTemplate>
                 </asp:DataList>
              </ContentTemplate>             
            </asp:UpdatePanel>
        </div>   





和cs文件





and cs file

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
         ...... other operation
    }


不要只是复制和粘贴。你可以使用CSS进行黑客攻击,它可以帮助你在div中正确定位图像按钮。



c# - div位置内的图像按钮 - 堆栈溢出 [ ^ ]
Don't just copy and paste.There is a hack that you can do with CSS and it will help you to position image buttons correctly inside div's.

c# - Image button inside div position - Stack Overflow[^]


这篇关于在运行时创建命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆