怎么在后台得到Repeater里的div标签?

一朝芳草碧连天 2011-12-07 11:33:00
<asp:Repeater ID="rep_2" runat="server" onitemdatabound="rep_2_ItemDataBound">
<ItemTemplate>
//外层 Repeater 内容
<asp:HyperLink ID="hk_One" runat="server" NavigateUrl='<%#Eval("Id","pro_2.aspx?Id={0}") %>' CssClass="STYLE4" ><div id="div_list" ><asp:Image ID="Image1" ImageUrl="~/image/title_2.png" CssClass="listImg" runat="server" />  <%#Eval("Name") %></div></asp:HyperLink>

<asp:Repeater ID="rep_3" runat="server" >
<ItemTemplate>
//内层Repeater 内容

<div id="<%#Eval("pid") %>" style="display:none"><asp:HyperLink ID="hk_Two" runat="server" NavigateUrl='<%#Eval("Id","pro_c.aspx?Id={0}") %>' CssClass="STYLE4" ><div id="div_proList_2" ><%#Eval("Name") %></div></asp:HyperLink></div>

</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>

我想在后台得到<div id="<%#Eval("pid") %>" style="display:none">,

但是总是不知道要 as 什么!

后台代码:
//左侧列表 --我在2里面找到rep_3 , 3里面包含着要得到的div.
protected void rep_2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string id = Request.QueryString["Id"];
Repeater rep = e.Item.FindControl("rep_3") as Repeater;
rep.FindControl(id);
}
}

我最终的目的是想让rep.FindControl(id); as 成一个div对象 ,可以修改他的Style;

我看了别的帖子,他们都是用JS..有点不一样. 注:是不是得不到div啊? 还是?
...全文
473 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
问题已经解决..最后在后台做的判断实现!

最用用到了一个用户控件.ascx;

前台代码:
<div id="div_Title"><img src="image/title_2.png" class="div_titleImg" /> 产品 <strong style="font-size:11px; font-family:'微软雅黑';">
Products</strong></div>
<div class="leftHr"></div>
<asp:Repeater ID="rep_2" runat="server" onitemdatabound="rep_2_ItemDataBound">
<ItemTemplate>
<asp:HyperLink ID="hk_One" runat="server" NavigateUrl='<%#Eval("Id","pro_2.aspx?Id={0}") %>' CssClass="STYLE4" ><div id="div_list" ><asp:Image ID="Image1" ImageUrl="~/image/title_2.png" CssClass="listImg" runat="server" />  <%#Eval("Name") %></div></asp:HyperLink>
<asp:Repeater ID="rep_3" runat="server">
<ItemTemplate>
<asp:HyperLink ID="hk_Two" runat="server" NavigateUrl='<%#Eval("Id","pro_c.aspx?Id={0}") %>' CssClass="STYLE4"><div id="div_proList_2" ><%#Eval("Name") %></div></asp:HyperLink>
<!-- rep_3结束 -->
</ItemTemplate>
</asp:Repeater>
<!-- rep_2结束 -->
</ItemTemplate>
</asp:Repeater>

还有原先的那样,没有任何变化.

后台:
public partial class pro_list : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] == null)
{
if (Request.QueryString["pid"] != null)
{
isExtend = true;

int childId = proInfoBLL.proInfo_SelectById(Convert.ToInt32(Request.QueryString["pid"]))[0].Pid;

GetWhichId(childId);
}
else
{
isExtend = false;
}
}
else
{
GetWhichId(Convert.ToInt32(Request.QueryString["id"]));
isExtend = true;
}
//左侧方案
this.rep_1.DataSource = solInfoBLL.SolInfo_Select();
this.rep_1.DataBind();

//左侧产品
this.rep_2.DataSource = proTieBLL.proTie_SelectPid();
this.rep_2.DataBind();
}

public void GetWhichId(int childId)
{
var proTie = proTieBLL.proTie_SelectById(childId)[0];

if (proTie.Pid == 0)
{
whichId = proTie.Id;
}
else
{
whichId = proTie.Pid;
}
}
bool isExtend = false;
int whichId = 0;

//在rep_2绑定时
protected void rep_2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep = e.Item.FindControl("rep_3") as Repeater;
proTie p = (proTie)e.Item.DataItem;
int pid = Convert.ToInt32(p.Id);
if (pid == whichId)
{
rep.DataSource = proTieBLL.proTie_SelectByPid(pid);
rep.DataBind();
}
}
}
}


因为关系什么的不是一下能解释清楚的,不解释关系了.代码都在这了!

谢谢帮助我的人.本论坛的朋友都是很热心的..

你们很热心啊..我爱你们...哈哈 特别鸣谢强哥的帮助!..谢谢你们
  • 打赏
  • 举报
回复
问题已经解决..最后在后台做的判断实现!

最用用到了一个用户控件.ascx;

前台代码:
<div id="div_Title"><img src="image/title_2.png" class="div_titleImg" /> 产品 <strong style="font-size:11px; font-family:'微软雅黑';">
Products</strong></div>
<div class="leftHr"></div>
<asp:Repeater ID="rep_2" runat="server" onitemdatabound="rep_2_ItemDataBound">
<ItemTemplate>
<asp:HyperLink ID="hk_One" runat="server" NavigateUrl='<%#Eval("Id","pro_2.aspx?Id={0}") %>' CssClass="STYLE4" ><div id="div_list" ><asp:Image ID="Image1" ImageUrl="~/image/title_2.png" CssClass="listImg" runat="server" />  <%#Eval("Name") %></div></asp:HyperLink>
<asp:Repeater ID="rep_3" runat="server">
<ItemTemplate>
<asp:HyperLink ID="hk_Two" runat="server" NavigateUrl='<%#Eval("Id","pro_c.aspx?Id={0}") %>' CssClass="STYLE4"><div id="div_proList_2" ><%#Eval("Name") %></div></asp:HyperLink>
<!-- rep_3结束 -->
</ItemTemplate>
</asp:Repeater>
<!-- rep_2结束 -->
</ItemTemplate>
</asp:Repeater>

还有原先的那样,没有任何变化.

后台:
public partial class pro_list : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] == null)
{
if (Request.QueryString["pid"] != null)
{
isExtend = true;

int childId = proInfoBLL.proInfo_SelectById(Convert.ToInt32(Request.QueryString["pid"]))[0].Pid;

GetWhichId(childId);
}
else
{
isExtend = false;
}
}
else
{
GetWhichId(Convert.ToInt32(Request.QueryString["id"]));
isExtend = true;
}
//左侧方案
this.rep_1.DataSource = solInfoBLL.SolInfo_Select();
this.rep_1.DataBind();

//左侧产品
this.rep_2.DataSource = proTieBLL.proTie_SelectPid();
this.rep_2.DataBind();
}

public void GetWhichId(int childId)
{
var proTie = proTieBLL.proTie_SelectById(childId)[0];

if (proTie.Pid == 0)
{
whichId = proTie.Id;
}
else
{
whichId = proTie.Pid;
}
}
bool isExtend = false;
int whichId = 0;

//在rep_2绑定时
protected void rep_2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep = e.Item.FindControl("rep_3") as Repeater;
proTie p = (proTie)e.Item.DataItem;
int pid = Convert.ToInt32(p.Id);
if (pid == whichId)
{
rep.DataSource = proTieBLL.proTie_SelectByPid(pid);
rep.DataBind();
}
}
}
}


因为关系什么的不是一下能解释清楚的,不解释关系了.代码都在这了!

谢谢帮助我的人.本论坛的朋友都是很热心的..

我爱你们...哈哈
  • 打赏
  • 举报
回复
现在就有一个问题, 用JS能出现效果, 父级有onclick事件,让子级显示,确实能显示出来;

但是每点击本页都会跳转,会把它们刷新没,,我想用onload事件,,但是完全没反映,像是调用不到一样,,

还有,点父级的时候,子级也就显示一个(应该有多个).. 怎么回事啊???子级Id都是一致的怎么会出来一个呢?

function show(id) {
childObj = document.getElementById(id);
alert(id);
if (childObj.style.display == 'none') {
childObj.style.display = 'block';
}
else {
childObj.style.display = 'none';
}
return;
}

我想写JS实现要怎么实现啊? 好像我这个本页跳转必须要用onload事件了.. 把onload时间加到哪儿好呢??
~~~~
  • 打赏
  • 举报
回复
嗯...ASP.NET服务器控件.它的ID必须是简单固定的,不让有服务器控件绑定..

还有我写的连接点击都在本页跳转的,它会刷新,根本走不到ItemCommmand事件 - -|||

我用AJAX了.也不好使. 正在努力处理中..
dongt1 2011-12-08
  • 打赏
  • 举报
回复
就这俩个方法 一个runat=“server” 另一个item.findcontrol("")还需要楼主亲自思考解决我们也只能够给出一些解决的思路,楼主加油。
关山明月 2011-12-08
  • 打赏
  • 举报
回复
我晕,还没出来么? 我上面的方法楼主估计也没试过。不知道是不是楼主要的功能,我做的是投票列表,用的repeater嵌套,每个投票标题前有个加号小图标,点击图标,就把它的所有选项显示出来,图标就成了减号了,再点击就隐藏选项了。显示隐藏是JS实现 的,不会刷新页面。 详Q我 779429663
begintransaction 2011-12-08
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 yan_san 的回复:]
引用 13 楼 jianghui7897 的回复:
在rowcommand事件里写
HtmlGenericControl div = e.Item.FindControl("id") as HtmlGenericControl;
id就是div的ID号,这样可以取出来



上面的我都看了,有的还在研究中,我试着用 HtmlGenericControl 了

结果(不知道图片能不……
[/Quote]
要是服务器控件哟,你加了runat="server"没?
liukaizxc 2011-12-07
  • 打赏
  • 举报
回复
HtmlGenericControl pid =it.FindControl(hf.value); HtmlGenericControl 这个类型包括所有的html标签
当然也包括div在内 所以 pid 就是你要的div
porschev 2011-12-07
  • 打赏
  • 举报
回复

把reapeater中的DIV换成一个panel

.cs文件用findcontrol去找这个panel

然后再进行操作。。。

PS:panel就是个DIV
zhangqihua1 2011-12-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 feng1366259807 的回复:]
得不到...但是你可以在绑定的时候通过判断 改变样式啊
[/Quote] 楼主的想法是正确的
liukaizxc 2011-12-07
  • 打赏
  • 举报
回复
Repeater rep = e.Item.FindControl("rep_3") as Repeater;
Hidden hf= e.Item.FindControl("hf") as Hidden ; // HTML 隐藏域 id为hf value='<%#Eval("pid")%>'
foreach(RepeatItem it in rep.Items)
{
HtmlGenericControl pid =it.FindControl(hf.value);//pid是用隐藏域保存值的话 可以获取到
pid.Style.Value = "color:red;";
}
风2013 2011-12-07
  • 打赏
  • 举报
回复
得不到...但是你可以在绑定的时候通过判断 改变样式啊
liukaizxc 2011-12-07
  • 打赏
  • 举报
回复
<div id="<%#Eval("pid") %>" style="display:none" runat="server">
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 jianghui7897 的回复:]
在rowcommand事件里写
HtmlGenericControl div = e.Item.FindControl("id") as HtmlGenericControl;
id就是div的ID号,这样可以取出来
[/Quote]


上面的我都看了,有的还在研究中,我试着用 HtmlGenericControl 了

结果(不知道图片能不能显示出来!):
C:\Users\Administrator\Desktop
HtmlGenericControl div = e.Item.FindControl("id") as HtmlGenericControl; 这个Id是有值的;


HtmlGenericControl div = e.Item.FindControl("id") as HtmlGenericControl; 但是对象值为null;

最后还是得不到啊~~~ 我刚试着用label做一个固定id一个text 我都as label了 也是得不到~
begintransaction 2011-12-07
  • 打赏
  • 举报
回复
http://www.15ae.com/archive/2011-12/07045843134.html
你看看这个,有个示例!
gxq323 2011-12-07
  • 打赏
  • 举报
回复
上下级的关系这种直接拼字符串吧, repeator太多了,太乱
jianghui7897 2011-12-07
  • 打赏
  • 举报
回复
后台:protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Repeater1.DataSource = new ManageSvice().GetBigInfo();
this.Repeater1.DataBind();
this.GridView1.DataSource = new ManageSvice().GetBigInfo();
this.GridView1.DataBind();
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "AddNew")
{
HtmlGenericControl div = e.Item.FindControl("HideDiv") as HtmlGenericControl;
div.Attributes.Add("style", "display:block;");
string OrderId = e.CommandArgument.ToString();
List<ChildInfo> infos = new ManageSvice().GetChildInfo(OrderId);

Button btn = e.Item.FindControl("AddBtn") as Button;
if (btn.Text == "+")
{
//div.Attributes.Add("style", "display:block;");
div.Visible = true;
btn.Text = "-";
Repeater Rp2 = e.Item.FindControl("Rp2") as Repeater;
Rp2.DataSource = infos;
Rp2.DataBind();
}
else
{
//div.Attributes.Clear();
//div.Attributes.Add("style", "display:none;");
div.Visible = false;
btn.Text = "+";
}

}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Addsc")
{
int Index =Convert.ToInt32(e.CommandArgument);
string OrderId = (this.GridView1.Rows[Index].Cells[0].Controls[3] as Label).Text;
List<ChildInfo> infos = new ManageSvice().GetChildInfo(OrderId);
Button btn = e.CommandSource as Button;
int s = 0; ;
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
if (this.GridView1.Rows[i].Cells[0].Controls[1].ClientID == btn.ClientID)
{
s = i;
}
}

GridView Rp2 = this.GridView1.Rows[Index].Cells[0].Controls[5] as GridView;

if (btn.Text == "+")
{
Rp2.Visible = true;
btn.Text = "-";

Rp2.DataSource = infos;
Rp2.DataBind();
}
else
{
//div.Attributes.Clear();
//div.Attributes.Add("style", "display:none;");
Rp2.Visible = false;
btn.Text = "+";
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("ondblclick", "alert('" + this.GridView1.DataKeys[e.Row.RowIndex].Values["OrderID"].ToString() + "','" + this.GridView1.DataKeys[e.Row.RowIndex].Values["OrderID"].ToString() + "')");
}
}
想要不刷新的话可以做成初始加载所有数据,然后点击的时候用JS设置DISPLAY属性
jianghui7897 2011-12-07
  • 打赏
  • 举报
回复
好吧。。刚好以前做过个类似的。。直接上代码了
<script type="text/javascript">
function GetVlue(crowindex)
{
document.getElementById("HideTxt").value=crowindex;
alert(document.getElementById("HideTxt").value);
}
var ms;
function SetColor(a)
{

a.style.backgroundColor="blue";
ms=a.style.backgroundColor;
}
function BackColor(c)
{

c.style.backgroundColor="yellow";
}

</script>
前台:<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="410px" onrowcommand="GridView1_RowCommand" DataKeyNames="OrderID"
onselectedindexchanged="GridView1_SelectedIndexChanged" onrowdatabound="GridView1_RowDataBound"
>
<Columns>
<asp:TemplateField>
<HeaderTemplate>

<table width="600px" border="1px solid" cellpadding="0" cellspacing="0">
<tr>
<td class="td1"> </td>
<td class="td2">订单编号</td>
<td class="td3">签单日期</td>
<td class="td4">销售员</td>
<td class="td5">签单日期</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="600px" border="1px">
<tr>
<td class="td1">
<asp:Button ID="Button1" runat="server" Text='+' CommandArgument='<%# Container.DataItemIndex %>' CommandName="Addsc" /></td>
<td class="td2">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("OrderID")%>'></asp:Label></td>
<td class="td3"><%#Eval("CompanyName")%></td>
<td class="td4"><%#Eval("EmpName")%></td>
<td class="td5"><%#Eval("OrderDate")%></td>

</tr>

<tr>
<td colspan="5">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
Width="508px">
<Columns>
<asp:TemplateField HeaderText="产品名称">
<ItemTemplate>
<%#Eval("ProductName")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="价格">
<ItemTemplate><%#Eval("UnitPrice")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="数量">
<ItemTemplate><%#Eval("Quantity")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="折扣">
<ItemTemplate><%#Eval("Discount")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ItemTemplate>
<%--<FooterTemplate></table></FooterTemplate>--%>
</asp:TemplateField>
</Columns>
</asp:GridView>


<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand" >
<HeaderTemplate>
<table width="600px" border="1px">
<tr>
<td id="td1"></td>
<td id="td2">订单编号</td>
<td id="td3">客户名称</td>
<td id="td4">销售员</td>
<td id="td5">签单日期</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td id="td1"><asp:Button ID="AddBtn" CommandArgument='<%#Eval("OrderID")%>' CommandName="AddNew" runat="server" Text="+" /></td>
<td id="td2"><%#Eval("OrderID")%></td>
<td id="td3"><%#Eval("CompanyName")%></td>
<td id="td4"><%#Eval("EmpName")%></td>
<td id="td5"><%#Eval("OrderDate")%></td>
<div id="HideDiv" runat="server">
<asp:Repeater ID="Rp2" runat="server">
<HeaderTemplate>
<table width="600px" border="1px">
<tr>
<td id="td1">产品名称</td>
<td id="td2">价格</td>
<td id="td3">数量</td>
<td id="td4">折扣</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr onmouseover="SetColor(this);" onmouseout="BackColor(this)">
<td id="td1"><%#Eval("ProductName")%></td>
<td id="td2"><%#Eval("UnitPrice")%></td>
<td id="td3"><%#Eval("Quantity")%></td>
<td id="td4"><%#Eval("Discount")%></td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</tr>

</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>



<br />
</form>
jianghui7897 2011-12-07
  • 打赏
  • 举报
回复
在rowcommand事件里写
HtmlGenericControl div = e.Item.FindControl("id") as HtmlGenericControl;
id就是div的ID号,这样可以取出来
关山明月 2011-12-07
  • 打赏
  • 举报
回复
直接用DIV <div id='<%#Eval("Vote_ID")%>' style="display:none"></div> 然后子级repeater放在这个DIV中,在第一个repeater的ItemDataBound事件

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rp = (Repeater)e.Item.FindControl("rplist1");

DataRowView drv = (DataRowView)e.Item.DataItem;
int id = Convert.ToInt32(((DataRowView)e.Item.DataItem).Row["Vote_ID"]);
rp.DataSource = VoteOptionListBind(id);
rp.DataBind();
}


这样就可以绑定了。
要实现点击显示隐藏的话就用JS。
加载更多回复(5)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧