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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25507622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:36:34  来源:igfitidea点击:

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

c#javascriptasp.netpostbackcustom-attributes

提问by Ceparu Stefan

How can I retrieve a Buttoncustom attribute after the attribute value has been changed using javascript?

Button使用 javascript 更改属性值后,如何检索自定义属性?

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

代码隐藏文件

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 Button1then I click Button2the actIndexvalue is still "1" but if I use page inspect the Button2actIndexattribute is "2", somehow the attribute value is not passed to postBack action.

如果我点击Button1然后我点击Button2actIndex价值仍然是“1”,但如果我使用页面检查Button2actIndex属性是“2”,不知何故属性值不传递到回传行动。

How can I solve this mystery?

我该如何解开这个谜团?

采纳答案by Kelsey

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 ViewStatebefore it serves up the page. Then you modify the value using javascript which has no effect because that vaule is not being posted back to the server. On PostBack, the server rebuilds the control from the known ViewStatewhich has the default value you originally assigned which is the value 1.

控件的状态在服务器端构建并存储在ViewState它提供页面之前。然后您使用 javascript 修改该值,该值无效,因为该值没有被回发到服务器。在回发时,服务器从已知的控件重建控件,该控件ViewState具有您最初分配的默认值,即 value 1

To get around this you need to store the value in some type of control (thinking a HiddenFieldcontrol) 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 修改控件,则需要手动处理您的控件。

回答by JJ_Coder4Hire

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();

回答by Kandarp Vyas

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

    }