在asp.net中动态添加datalist时出错 [英] Error in dynamically added datalist in asp.net

查看:85
本文介绍了在asp.net中动态添加datalist时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我面临动态添加的数据列表问题。 Client_Id给控件的不同。



这是我的代码:



面板:



Panel otherpanin = new Panel();

otherpanin.ID =otherpan;

otherpanin。 BackColor = Color.LightGreen;

otherpanin.GroupingText =可用选项;

otherpanin.ClientIDMode = ClientIDMode.AutoID;

newpan.Controls .Add(otherpanin);

DataList otherdlin = new DataList();

otherdlin.ItemTemplate = new BindItems();

otherdlin.GridLines = GridLines.Both;

otherdlin.RepeatDirection = RepeatDirection.Horizo​​ntal;

otherdlin.RepeatColumns = 3;

otherdlin.ItemDataBound + = new DataListItemEventHandler (Item_Bound);

otherdlin.DataSource = Othe rRows;

otherdlin.DataBind();

otherpanin.Controls.Add(otherdlin);



< br $>
项目模板:



项目模板

公共类BindItems:ITemplate

{

public BindItems()

{

//添加构造函数

}

public void InstantiateIn(控件容器)

{



Label lblName = new Label();

lblName。 ID =lblName;





DropDownList ddlYesNo = new DropDownList();

ddlYesNo.ID = ddlYesNo;

ddlYesNo.Items.Add(new ListItem(No,N));

ddlYesNo.Items.Add(new ListItem(是) ,Y));





TextBox txtData = new TextBox();

txtData。 ID =txtData;



container.Controls.Add(lblName);

container.Controls.Add(ddlYesNo);

container.Controls.Add(txtData);

}







}





物品绑定事件:



其物品绑定

protected void Item_Bound(Object sender,DataListItemEventArgs e)

{



if(e.Item.ItemType == ListItemType.Item ||

e.Item.ItemType == ListItemType.AlternatingItem)

{





Label lblName =(Label)e.Item.FindControl(lblName);

lblName.Text =((DataRowView)e.Item.DataItem).Row.ItemArray [0] .ToString ();

lblName.Width = 120;



DropDownList ddlYesNo =(DropDownList)e.Item.FindControl(ddlYesNo);

ddlYesNo.Width = 50;

TextBox txtData =(TextBox)e.Item.FindControl(txtData);

txtData.Width = 40;



if(((DataRowView)e.Item.DataItem).Row.ItemArray [1] .ToString()==Y)

{

ddlYesNo.Attributes.Add(onchange,Javascript:OnDropdownChange(''+ ddlYesNo.ClientID +'',''+ txtData.ClientID + ''));

}

else if(lblNam e.Text ==百分比选项)

{

txtData.Text = optioncode;

ddlYesNo.Visible = false;

txtData.Enabled = false;

txtData.Width = 90;

}

else

{

ddlYesNo.Visible = false;

txtData.Enabled = true;

}







}



}





错误:

 <   td  >  <   span     id   =   ctl04_lblNa me_3    style   =  display:inline-block; width:120px; >  CS INFANT <   / span  >  <  选择    name   =  ctl04 $ ctl03 $ ddlYesNo    id   =  ctl04_ddlYesNo_3 < span class =code-attribute>   onchange   = 使用Javascript:OnDropdownChange(安培;#39;&ctl03_ddlYesNo_3放大器;#39;,&安培;#39; ctl03_tx tData_3&#39;)    style   =  width:50px; >  
< 选项 = N > < / option >
< 选项 value = Y > &l t; / option >

< / select >











观察者cotnrol的id为:ctl04_ddlYesNo_3但是当发送到javascript它显示为tl03_ddlYesNo_3





你能帮助我吗。



先谢谢。



问候

Govardhan

解决方案

ctl03


ddlYesNo id = ctl04_ddlYesNo_3 onchange = Jav ascript:OnDropdownChange(&#39; ctl03_ddlYesNo_3&#39;,&#39; ctl03_txtData_3&#39;) style = width:50px; >
< 选项 value = N > < / option >
< 选项 value = Y > < / option >

< / select >











观察者cotnrol的id是:ctl04_ddlYesNo_3但是当发送到javascript时它显示为tl03_ddlYesNo_3





你能帮帮我吗。



提前致谢。



问候

Govardhan






读你的文章你有问题是当下拉列表更改时,客户端javascript show是执行但不是。



查看代码,在我看来,我认为,你的代码意味着你从服务器端获取客户端'ID。但是你是否可以从服务器端获取客户端'ID。你最好将标签client''id放在一个隐藏的标签中并从服务器端获取隐藏的值。



我不确定是否可以成功。也许你可以自己试试。



我不擅长英语。



谢谢

Hi,

I am facing a issue with the dynamically added datalist. Client_Id''s giving to the controls are different.

Here is my code:

Panel:

Panel otherpanin = new Panel();
otherpanin.ID = "otherpan";
otherpanin.BackColor = Color.LightGreen;
otherpanin.GroupingText = "Available Options";
otherpanin.ClientIDMode = ClientIDMode.AutoID;
newpan.Controls.Add(otherpanin);
DataList otherdlin = new DataList();
otherdlin.ItemTemplate = new BindItems();
otherdlin.GridLines = GridLines.Both;
otherdlin.RepeatDirection = RepeatDirection.Horizontal;
otherdlin.RepeatColumns = 3;
otherdlin.ItemDataBound += new DataListItemEventHandler(Item_Bound);
otherdlin.DataSource = OtherRows;
otherdlin.DataBind();
otherpanin.Controls.Add(otherdlin);


Item Template:

Item Template
public class BindItems : ITemplate
{
public BindItems()
{
//Add constructor
}
public void InstantiateIn(Control container)
{

Label lblName = new Label();
lblName.ID = "lblName";


DropDownList ddlYesNo = new DropDownList();
ddlYesNo.ID = "ddlYesNo";
ddlYesNo.Items.Add(new ListItem("No", "N"));
ddlYesNo.Items.Add(new ListItem("Yes", "Y"));


TextBox txtData = new TextBox();
txtData.ID = "txtData";

container.Controls.Add(lblName);
container.Controls.Add(ddlYesNo);
container.Controls.Add(txtData);
}



}


Item Bound event:

Its Item Bound
protected void Item_Bound(Object sender, DataListItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{


Label lblName = (Label)e.Item.FindControl("lblName");
lblName.Text = ((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString();
lblName.Width = 120;

DropDownList ddlYesNo = (DropDownList)e.Item.FindControl("ddlYesNo");
ddlYesNo.Width = 50;
TextBox txtData = (TextBox)e.Item.FindControl("txtData");
txtData.Width = 40;

if (((DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString() == "Y")
{
ddlYesNo.Attributes.Add("onchange", "Javascript:OnDropdownChange(''" + ddlYesNo.ClientID + "'',''" + txtData.ClientID + "'')");
}
else if(lblName.Text == "Percentage Option")
{
txtData.Text = optioncode;
ddlYesNo.Visible = false;
txtData.Enabled = false;
txtData.Width = 90;
}
else
{
ddlYesNo.Visible = false;
txtData.Enabled = true;
}



}

}


Error:

<td><span id="ctl04_lblName_3" style="display:inline-block;width:120px;">CS INFANT</span><select name="ctl04$ctl03$ddlYesNo" id="ctl04_ddlYesNo_3" onchange="Javascript:OnDropdownChange(&#39;ctl03_ddlYesNo_3&#39;,&#39;ctl03_txtData_3&#39;)" style="width:50px;">
                                <option value="N">No</option>
                                <option value="Y">Yes</option>

                            </select>






Observer that Id of the cotnrol is : ctl04_ddlYesNo_3 but when sent to javascript it is showing as tl03_ddlYesNo_3


Can you help me out.

Thanks in advance.

Regards
Govardhan

解决方案

ctl03


ddlYesNo" id="ctl04_ddlYesNo_3" onchange="Javascript:OnDropdownChange(&#39;ctl03_ddlYesNo_3&#39;,&#39;ctl03_txtData_3&#39;)" style="width:50px;"> <option value="N">No</option> <option value="Y">Yes</option> </select>






Observer that Id of the cotnrol is : ctl04_ddlYesNo_3 but when sent to javascript it is showing as tl03_ddlYesNo_3


Can you help me out.

Thanks in advance.

Regards
Govardhan


Hi

Read you article you problem is that when the dropdownlist changed the client javascript show be execute but is is not.

View you code ,in my opinion ,I think that ,you code means that you get the client''ID from server side. but whether you can get the client''ID from the server side. you''d better put the tag client''id in one hidden tag and get the hidden value from the server side.

I am not sure is can be success.Maybe you can try it by yourself.

I am not good at in English.

Thanks.


这篇关于在asp.net中动态添加datalist时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆