简体   繁体   English

如何添加 ASP.NET 控件在 Html 中标记并在 .cs 上的 .FindControl() 方法中分配它们?

[英]How to add ASP.NET control for <i></i> tags in Html and assign them inside .FindControl() method on .cs?

Does anyone knows How to add ASP.NET control for <i></i> tags?有谁知道如何为<i></i>标签添加 ASP.NET 控件?
i am looking for ASP.NET Control that is equivalent with <i> Html tag我正在寻找与<i> Html 标记等效的 ASP.NET 控件

in my case, I also want to implement .FindControl() method for it inside the .cs file.就我而言,我还想在.cs文件中为它实现 .FindControl() 方法。
Here's the part of the code..这是代码的一部分..


Example.axcs例子.axcs

<asp:Panel id="forDiv" CssClass="input-group colorpicker-component" runat="server">
                        <asp:TextBox ID="txtHeree" class="form-control" runat="server" Style="width: auto"></asp:TextBox>
                        <asp:Panel CssClass="input-group-append" runat="server">
                            <asp:Label CssClass="input-group-addon input-group-text" runat="server"><i></i></asp:Label>
                        </asp:Panel>
</asp:Panel>


from the code above, the FindControl method that I have implemented goes like this,从上面的代码中,我实现的 FindControl 方法是这样的,


Example.ascx.cs例子.ascx.cs

TextBox txtVal = e.Row.FindControl("txtHeree") as TextBox;
Panel txtVal = e.Row.FindControl("forDiv") as Panel;

and, etc..等等..


My current problem is, I still can't find both the equivalent ASP.NET Control for <i></i> tags and also for .FindControl() method for it.我目前的问题是,我仍然找不到<i></i>标记的等效 ASP.NET 控件以及它的 .FindControl() 方法。
sure, I have read the reference to another QnA forum from both Stackoverflow and Microsoft Docs:当然,我已经阅读了 Stackoverflow 和 Microsoft Docs 中对另一个 QnA 论坛的引用:
ASP.NET Control to HTML tag equivalent ASP.NET 控件到 HTML 标记等效
and
https://docs.microsoft.com/en-us/troubleshoot/developer/webapps/aspnet/development/server-controls https://docs.microsoft.com/en-us/troubleshoot/developer/webapps/aspnet/development/server-controls

ANY suggestion, answer, or efficient code for the <i></i> tag control, will be very.. very helpful. <i></i>标签控件的任何建议、答案或有效代码都将非常...非常有帮助。

Thank You.谢谢你。

Sure, if you add a "id" tag, and runat=server, then that "i" thing becomes quite much a server side control.当然,如果你添加一个“id”标签,并且 runat=server,那么那个“i”就变成了一个服务器端控件。

And doing that allows use in listview, repeaters etc.这样做允许在列表视图、中继器等中使用。

So, do this:所以,这样做:

        <i id="MyIguy" runat="server"></i>
        <br />

        <asp:Button ID="Button1" runat="server" Text="Button" 
            CssClass="btn" OnClick="Button1_Click" />

So, with code behind the button like this:因此,按钮后面的代码如下:

    protected void Button1_Click(object sender, EventArgs e)
    {
        MyIguy.InnerText = "this is fun";

    }

And we see this when run:我们在运行时看到了这一点:

在此处输入图像描述

And say we use a gridview, repeater etc.?假设我们使用gridview、repeater 等?

say this markup, we just dropped in that (wiht id, and runat=server).说这个标记,我们刚刚加入(带有 id 和 runat=server)。

Quite much makes that control work, look, and feel like any other server side asp.net control.相当多地使该控件的工作、外观和感觉与任何其他服务器端 asp.net 控件一样。

Say, this markup and gridview (for the find control example).比如说,这个标记和网格视图(对于查找控件示例)。

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
            DataKeyNames="ID" CssClass="table" >
            <Columns>
                <asp:TemplateField HeaderText="Hotel Name"> <ItemTemplate>
                        <asp:Label ID="txtHotel" runat="server" Text='<%# Eval("HotelName") %>' ></asp:Label>
                    </ItemTemplate> </asp:TemplateField>

                <asp:TemplateField HeaderText="City"> <ItemTemplate>
                        <asp:Label ID="txtCity" runat="server" Text='<%# Eval("City") %>' ></asp:Label>
                    </ItemTemplate> </asp:TemplateField>

                <asp:TemplateField HeaderText="Description"> <ItemTemplate>
                        <i id="Descript" runat="server"> <%# Eval("Description") %>  </i>
                    </ItemTemplate> </asp:TemplateField>

                <asp:TemplateField HeaderText="View"> <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" Text="View" 
                        CssClass="btn" OnClick="Button1_Click" />
                    </ItemTemplate> </asp:TemplateField>

            </Columns>
        </asp:GridView>

Code to load:要加载的代码:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = General.MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName");
            GridView1.DataBind();
        }
    }

And we have this now:我们现在有了这个:

在此处输入图像描述

And our view button click code, say this:而我们的查看按钮点击代码,这样说:

    protected void Button1_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        GridViewRow gRow = btn.NamingContainer as GridViewRow;

        int? PKID = GridView1.DataKeys[gRow.RowIndex]["ID"] as int?;

        Debug.Print("pk data base row id = " + PKID.ToString());
        Debug.Print("Row click index = " + gRow.RowIndex);

        // get hotel name
        Label txtHotel = gRow.FindControl("txtHotel") as Label;
        Debug.Print("Hotel name = " + txtHotel.Text);

        // get our <i> guy
        HtmlGenericControl myIthing = gRow.FindControl("Descript") as HtmlGenericControl;
        Debug.Print("Our i guy = " + myIthing.InnerText);

    }

Output :输出 :

在此处输入图像描述

so, most such controls - adding a "id" and runat="server", and at that point, it quite much behaves like most other asp.net controls.因此,大多数这样的控件 - 添加一个“id”和 runat="server",在这一点上,它的行为与大多数其他 asp.net 控件非常相似。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在asp.net中处理动态控制(findcontrol) - handle dynamic control in asp.net (findcontrol) 如何动态地将html元素添加到页面并在asp.net中访问它们? - How do I dynamically add html elements to a page and access them in asp.net? 如何以编程方式将ASCX中的ASP.NET控件添加到外部RequiredFieldValidator? - How do I add an ASP.NET control inside an ASCX to an external RequiredFieldValidator programmatically? 如何将我在中继器中选择的 DropDownList 值添加到 ASP.Net 中的列表框控件 - How Can add I selected value of DropDownList inside a Repeater to the listbox control in ASP.Net ASP.NET,C#,如何动态识别用户控件的属性或方法并为其添加价值? - ASP.NET, C#, How to dynamically identify the properties or method of a user control and add value to them? asp.net c# FindControl inside Reapter inside HTML table return null - asp.net c# FindControl inside Reapater inside HTML table return null ASP.Net FindControl不工作 - 怎么样? - ASP.Net FindControl is not working - How come? 如何在没有Shift,Control或Alt要求的情况下为asp.net按钮分配键盘快捷键? - How do I assign keyboard shortcuts to asp.net buttons without Shift, Control, or Alt requirements? 如何使用C#将位图分配给ASP.NET中的Image控件? - How can I assign a bitmap to a Image control in asp.net using c#? 控件不存在时的 Asp.Net.findcontrol() - Asp.Net .findcontrol() when control doesn't exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM