简体   繁体   English

设置ASP.NET Button属性客户端和读取属性值服务器端

[英]Setting ASP.NET Button attributes client side and read attribute value server side

How can I retrieve a Button custom attribute after the attribute value has been changed using javascript? 在使用javascript更改属性值后,如何检索Button自定义属性?

Example: 例:

Asp file Asp文件

<asp:Button ID="Button1" runat="server" Text="Button1" />
<asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" />

<script type="text/javascript">
var btn1 = '#<% Button1.ClientID %>';
var btn2 = '#<% Button2.ClientID %>';

$(btn1).click(function(e) {
    e.preventDefault();
    $(btn2).attr("actIndex", "2");
});

</script>

CodeBehind file CodeBehind文件

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        Button2.Attributes.Add("actIndex","1");
}

protected void Button2_Click(object sender, EventArgs e)
{
     Button btn = (Button)sender;

     // this should be 2 if button1 has been clicked
     string actIndex = btn.Attributes["actIndex"]; 
}

If I click Button1 then I click Button2 the actIndex value is still "1" but if I use page inspect the Button2 actIndex attribute is "2", somehow the attribute value is not passed to postBack action. 如果我单击Button1然后我单击Button2actIndex值仍然是“1”但如果我使用页面检查Button2 actIndex属性是“2”,不知何故属性值不传递给postBack操作。

How can I solve this mystery? 我该如何解开这个谜团?

I think the problem you have is because the attributes are not being posted back to have their information rebuilt server side. 我认为你遇到的问题是因为没有回发属性以重建服务器端的信息。

The control's state is built server side and stored in the ViewState before it serves up the page. 控件的状态是服务器端构建的,并在提供页面之前存储在ViewState Then you modify the value using javascript which has no effect because that vaule is not being posted back to the server. 然后使用javascript修改该值,该值无效,因为该vaule未被回发到服务器。 On PostBack, the server rebuilds the control from the known ViewState which has the default value you originally assigned which is the value 1 . 在PostBack上,服务器从已知的ViewState重建控件,该ViewState具有您最初分配的默认值,即值1

To get around this you need to store the value in some type of control (thinking a HiddenField control) that will get posted back to the server and then rebuild the attribute server side. 为了解决这个问题,你需要将值存储在某种类型的控件中(想一个HiddenField控件),它将被发布回服务器,然后重建属性服务器端。

eg (semi pseudo code): 例如(半伪代码):

// In your javascript write to a hidden control
$("#yourHiddenFieldControlName").val("2");

// Then in your server side c# code you look for this value on post back and if found,
// assign it to you attribute
if (!string.IsNullOrEmpty(yourHiddenFieldControlName.Value))
{
    Button2.Attributes["actIndex"] = yourHiddenFieldControlName.Value;
} 

Your control needs to be handled manually if you are modifying it client side with javascript. 如果您使用javascript修改客户端,则需要手动处理您的控件。

only form elements can actually postback data. 只有表单元素才能实际回发数据。 The server side will take the postback data and load it into the form element provided that the runat=server is set. 如果设置了runat = server,服务器端将获取回发数据并将其加载到表单元素中。

in markup or html: 在标记或HTML中:

<input type="hidden" runat="server" ID="txtHiddenDestControl" />

javascript: JavaScript的:

document.getElementById('<%= txtHiddenDestControl.ClientID %>').value = '1';

code behind: 代码背后:

string postedVal = txtHiddenDestControl.Value.ToString();

NO need for Javascript below code will work for you 不需要Javascript下面的代码将适合您

 protected void Page_Load(object sender, EventArgs e)
    {
        Button2.Attributes.Add("actIndex", "1");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Value = Button2.Attributes["actIndex"].ToString();//1
        Button2.Attributes.Remove("actIndex");
        Button2.Attributes.Add("actIndex", "2");
         Value = Button2.Attributes["actIndex"].ToString();//2

    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM