C#中的JavaScript问题 [英] JavaScript Problem In C#
问题描述
function SetPlan_Id(LblPlanId) {
var PlanId = 'MainContent_GvGrid_' + LblPlanId;
document.getElementById('<%= HPlanId.ClientID %>').innerHTML = document.getElementById(PlanId).innerHTML
这个函数在VB.NET中运行良好......但不是在C#中。 。
因为我从这里生成了硬编码的id ....
那么C#的解决方案是什么..
这个函数是代码背后的调用....
this function is Working Fine In VB.NET...But Not In C#..
Because I Generated Hard Coded id From Here....
So What is the solution For C#..
this Function Is Call at Code Behind....
protected void GvGrid_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label LblId = (Label)e.Row.FindControl("LblId");
Panel pnl1 = (Panel)e.Row.FindControl("pnl1");
string Id = LblId.ClientID;
pnl1.Attributes.Add("onmouseover", "SetPlan_Id('" + Id + "');");
}
}
推荐答案
I think you don't need this part in javascript:
var PlanId = 'MainContent_GvGrid_' + LblPlanId;
因为你已经发送了lbl.ClientId作为参数...只需调用
document.getElementById('<%= HPlanId.ClientID%>')。innerHTML = document.getElementById(LblPlanId).innerHTML
通常,您永远不希望为服务器端控件提供用于ID的硬编码字符串。如果你需要C#版本(应该是相同的,真的,除了C#是区分大小写的 - 最愚蠢的语言属性)你必须检查页面源并查看页面标签的id是什么已满载。
只是一个小抱怨:javascript一般不是关于C#和VB.NET,它是客户端代码而另外两个是服务器端。
Since you're already sending lbl.ClientId as parameter...just call
document.getElementById('<%= HPlanId.ClientID %>').innerHTML = document.getElementById(LblPlanId).innerHTML
In general you never want to have hard-coded string for ID for server side controls. If you need C# version (should be the same, really, except C# is case sensitive - stupidest language property ever) you have to check the page source and see what is the id of the label once the page is fully loaded.
Just a minor gripe: javascript generally isn't about C# and VB.NET, it is client side code while other two are server side.
尝试使用LblId.Text而不是LblId.ClientId,因为LblId是放置在gridview内的服务器端控件。
protected void GvGrid_RowCreated(object sender,System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Label LblId =(Label)e.Row.FindControl(LblId);
Panel pnl1 =(面板)e.Row.FindControl(pnl1);
字符串Id = LblId.Text;
pnl1.Attributes.Add(onmouseover,SetPlan_Id('+ Id +'););
}
}
Try to use LblId.Text instead of LblId.ClientId as LblId is a server side control which placed inside the gridview.
protected void GvGrid_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label LblId = (Label)e.Row.FindControl("LblId");
Panel pnl1 = (Panel)e.Row.FindControl("pnl1");
string Id = LblId.Text;
pnl1.Attributes.Add("onmouseover", "SetPlan_Id('" + Id + "');");
}
}
这篇关于C#中的JavaScript问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!