2014-04-24 60 views
-1

我在我的aspx頁面中有一箇中繼器控件,並且在那個頁面中我放置了複選框。當用戶選中此框時,我想重定向到一個頁面。我已經寫了一個JavaScript也執行此操作如下:如何動態地將參數傳遞給javascript

JS:

function update(eid) { 
     window.location("Events.aspx?eid="+eid); 
    } 

下面是代碼的方法後面:

protected void rptEventReminder_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
     CheckBox cbx = e.Item.FindControl("chkComplete") as CheckBox; 
     Label lbl = e.Item.FindControl("lblEid") as Label; 

     if (cbx != null && lbl !=null) 
     { 
      Int64 eid = Convert.ToInt64(lbl.Text); 
      cbx.Attributes.Add("onclick", "update(eid);"); 
     } 
    } 

,我想過去EID這因爲參數是數據庫中唯一的。

JavaScript錯誤我得到的是:

JavaScript runtime error: 'eid' is undefined

+0

試試這個:cbx.Attributes.Add(「onclick」,「update('」+ eid +「');」); –

回答

3

目前,你傳入eid作爲硬編碼的文本onclick處理程序,它會將其作爲JavaScript變量因此您收到錯誤

現在

JavaScript runtime error: 'eid' is undefined

,在C#代碼eid是一個變量因此,你需要把它作爲

cbx.Attributes.Add("onclick", "update(" + eid +");"); 
+0

嗨@Satpal謝謝你的傢伙...謝謝你的回覆 –

0

我找到了解決我的問題。我在後面的代碼後面做了一些小改動,如下所示:

protected void rptEventReminder_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    CheckBox cbx = e.Item.FindControl("chkComplete") as CheckBox; 
    Label lbl = e.Item.FindControl("lblEid") as Label; 

    if (cbx != null && lbl !=null) 
    { 
     Int64 eid = Convert.ToInt64(lbl.Text); 
     cbx.Attributes.Add("onclick", "update("+eid+");"); 
    } 
} 

現在它的工作正常。

+0

你在'eid'後面缺少'+' – Satpal

+0

謝謝@Satpal,我糾正了我的錯誤...... –

相關問題