我怎样才能HTML属性从ASP / vb.net codebehind [英] how can I get html attribute from ASP/vb.net Codebehind

查看:113
本文介绍了我怎样才能HTML属性从ASP / vb.net codebehind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从ASP更改:TabContainer的自举NAV-标签

I changed from asp:tabcontainer to bootstrap nav-tabs

<div id="Tabs2" runat="server" role="tabpanel">
                <ul class="nav nav-tabs" role="tablist">
                    <li class="active"><a href="#card" aria-controls="card" role="tab" data-toggle="tab" tabindex="0">Card</a></li>
                    <li><a href="#term" aria-controls="term" role="tab" data-toggle="tab" tabindex="1">Term</a></li>
                    <li><a href="#account" aria-controls="account" role="tab" data-toggle="tab" tabindex="2">Account</a></li>
                    <li><a href="#counter" aria-controls="counter" role="tab" data-toggle="tab" tabindex="3">Counter</a></li>
                    <li><a href="#allsust" aria-controls="allsust" role="tab" data-toggle="tab" tabindex="4">All Suspect</a></li>

                </ul>
            </div

然后我就需要从HTML获取的tabindex检查情况
这是code从我的asp:TabContainer的

then i just need to get tabindex from html to check condition this is code from my asp:tabcontainer

Protected Sub imgPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles imgPrev.Click
    GridView1.PageIndex = 0
    TextDate.Text = Func.Date2YYMMDD(DateAdd(DateInterval.Day, -1, BizFunc.YYMMDD2Date(TextDate.Text)))
    Select Case TabContainer1.ActiveTabIndex
        Case 0 : btnSearchCard_Click(sender, e)
        Case 1 : btnSearchTerm_Click(sender, e)
        Case 2 : btnSearchAccount_Click(sender, e)
        Case 3 : btnSearchCounter_Click(sender, e)
        Case 4 : btnSearchSuspect_Click(sender, e)
    End Select
End Sub

我试图用 Tabs2.attribute ,但我不知道我应该做的下一步该怎么

i tried to use Tabs2.attribute but i don't know what next i should do

推荐答案

您无法访问该链接标记&LT; A&GT; 在ASP.NET服务器,除非它有在 =服务器属性。这里是你的选择:

You cannot access the link tag <a> from the server in ASP.NET unless it has the runat="server" attribute. Here are your options:

如果你不介意让ASP渲染标签为你,只用本民族的超链接控制。

If you don't mind letting ASP render the tags for you, just use their native hyperlink control.

优点:您可以访问和使用的CssClass和JavaScript,CSS等的组合,从服务器和客户端操作元素

PROS: You can access and manipulate the element from server and client side using a combination of CssClass and javascript, CSS, etc.

缺点:的标签的确会由服务器进行渲染,所以如果你有一个前端工程师与您合作,他可能是处于劣势,如果他想修改布局

CONS: The tags would indeed be rendered by the server, so if you have a front-end engineer working with you, he might be at a disadvantage if he wanted to modify the layout.

创建一个&LT; ASP:HiddenField&GT; 里面的每一个都等于对应&LT的的tabindex值列表中的项目;一&GT ; 属性

Create a <ASP:HiddenField> inside each of the list items that have a value equal to the tabindex of the corresponding <a> attribute.

优点::您可以使用JavaScript来设置HiddenField的值,所以如果你动态地构建领域,它会工作的。此外,没有服务器端渲染的元素是一样在需要选择#1。这个,如果你是体面的使用JavaScript实际上可能是最优雅的解决方案。

PROS: You can use javascript to set the value of a HiddenField, so if you're building the field dynamically, it would work out. Also, no server-side element rendering is necessary like in option #1. This actually might be the most elegant solution if you are decent with javascript.

缺点:服务器访问实际的选项卡索引属性会从相应的&LT脱钩; A&GT; 标记。这充其量是一个额外的警告该网页上工作时,要记住,在最坏的情况会导致意外的行为,异常集结错误的(可能不是)的,等以后。

CONS: The actual tab-index attribute accessed by the server would be decoupled from its corresponding <a> tag. This will at best be an additional caveat to remember when working on that page, and at worst will cause unexpected behavior, exceptions, build-errors (probably not), etc. later on.

这将允许服务器以查看和访问该元素(尽管可能需要使用一个或两个DirectCast函数)。我会包括这个答案的最后一些code,显示的方法,无需转换功能。

This would allow the server to see and access the element (albeit you may have to use one or two DirectCast functions). I'll include some code at the end of this answer that shows a method without conversion functions.

优点:简单。可能会完成你想在这种特殊情况下该怎么做。

PROS: Simple. Will likely accomplish what you're trying to do in this specific case.

缺点:包装服务器控件里面的时候HtmlGenericControls,所以你必须记住这一点,如果你决定与前端code做多存在一些注意事项。为了避免这种情况,你可能不得不改变你的&LT; D​​IV&GT; 包装成&LT; ASP:占位符&GT; 标签 - 我相信被呈现为一个DIV反正

CONS: Some caveats exist when wrapping server controls inside HtmlGenericControls, so you'll have to keep this in mind if you decide to do more with the front-end code. To avoid this, you'll probably have to change your <div> wrapper into a <asp:PlaceHolder> tag--which I believe gets rendered as a div anyway.

如果你想要去使用选项#3,这里是你会怎么做示范:

If you want to go with option #3, here is a demonstration of how you would do it:

HTML

<asp:PlaceHolder runat="server" ID="test_PH">
            <ul id="test_UL">
                <li><a id="link1_undetectable" href="#" tabindex="0">Tab 0 - This will not be detected.</a></li>
                <li><a id="link2_undetectable" href="#" tabindex="1">Tab 1 - This will not be detected.</a></li>
                <li><a id="link3_undetectable" href="#" tabindex="2">Tab 2 - This will not be detected.</a></li>
                <li><a id="link4_undetectable" href="#" tabindex="3">Tab 3 - This will not be detected.</a></li>
                <li><a id="link5_undetectable" href="#" tabindex="4">Tab 4 - This will not be detected.</a></li>
                <li><a id="link6" href="#" runat="server" tabindex="5">Tab 5</a></li>
                <li><a id="link7" href="#" runat="server" tabindex="6">Tab 6</a></li>
                <li><a id="link8" href="#" runat="server" tabindex="7">Tab 7</a></li>
                <li><a id="link9" href="#" runat="server" tabindex="8">Tab 8</a></li>
                <li><a id="link10" href="#" runat="server" tabindex="9">Tab 9</a></li>
            </ul>
        </asp:PlaceHolder>

VB.NET

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim ph As PlaceHolder = test_PH, sb As New StringBuilder() ' The string builder is just for demonstration purposes.

        For Each c As HtmlAnchor In ph.Controls.OfType(Of HtmlAnchor)() ' This will loop through the HtmlAnchors without throwing an exception.
            sb.AppendLine(String.Format("ID: ""{0}""; Tab-index: ""{1}""", c.ID, c.Attributes("tabindex"))) ' This builds a string for demo purposes.
        Next

        MsgBox(sb.ToString) 'Show the string for demo purposes.
    End Sub


结果...

这篇关于我怎样才能HTML属性从ASP / vb.net codebehind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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