设置按钮通过将代码后面的按钮ID传递给Jquery函数来显示无属性?

时间:2016-04-04 10:16:50

标签: javascript jquery asp.net .net

我有一个代码隐藏功能,它给了我一个按钮ID。现在我想使用jQuery隐藏该特定按钮。这是我背后的代码

if (dt.Rows.Count > 0) {
    for (int i = 2; i < dt.Columns.Count; i++) {
        int status = Convert.ToInt32(dt.Rows[0][i].ToString());
        if (status == 1) {
            string columnname = dt.Columns[i].ToString(); //here column name is button id
            ClientScript.RegisterStartupScript(this.GetType(), "getid", "getid('" + columnname + "');", true);
        }
    }
}

这是我未能制作的jQuery函数:

$(document).ready(function () {
    function getid(id){
        $(id).toggle('hide');
    } 
});

1 个答案:

答案 0 :(得分:0)

You might try ensuring that no errors are appearing within the Developer Tools (F12) within your browser (check the Console section).

Additionally, since your function is being defined within your jQuery "document-ready" block, it might not be available whenever your startup scripts are triggering. You could try to resolve this in one of the following ways :

Try Defining Your Function Outside of the jQuery Section

function getid(id){
    $(id).toggle('hide');
} 

Additionally, if you didn't want to rely on jQuery at all, you could simply use the following snippet of code :

function getid(id){
     var e = document.getElementById(id);
     e.style.display = (e.style.display == "block") ? 'none' : 'block';
} 

Consider Adding a Delay

In my experience, I've found that the RegisterStartupScript method can often fire off before what you are calling is actually ready to be called. You can resolve this by adding a slight delay to the call itself using the setTimeout() function :

// Set a 10ms delay prior to calling your getid function
ClientScript.RegisterStartupScript(GetType(), "getid", "setTimeout(function(){ getid('" + columnname + "');},10);", true);