C# 在 NavigateUrl 的中继器控件中放置我的代码中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/790629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Placing a variable from my Code Behind inside Repeater Control for NavigateUrl
提问by Etienne
This is my Repeater
这是我的中继器
<asp:Repeater ID="blogRepeater" runat="server">
<ItemTemplate>
<br />
<asp:Image ID="Image1" runat="server" Height="56px" ImageUrl='<%= string.Format( My_Variable) %>' Width="64px" />
<asp:HyperLink ID="HyperLink2" runat=server NavigateUrl='<%#Eval("Company_ID", "CompanyProfile.aspx?ID={0}")%>'><%#Eval("Name")%></asp:HyperLink>
<br />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
This is my Code behind in Page Load
这是我在页面加载中的代码
' Define data objects
Dim conn As Data.SqlClient.SqlConnection
Dim Comm As Data.SqlClient.SqlCommand
Dim reader As Data.SqlClient.SqlDataReader
conn = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Comm = New Data.SqlClient.SqlCommand( _
("SELECT Company_ID, Name FROM CompanyTable ORDER BY Name"), conn)
Dim My_Variable As String
My_Variable = "~/createthumb.ashx?gu=/images/Logo.bmp" + "&xmax=75&ymax=75"
' Open the connection
conn.Open()
' Execute the category command
reader = Comm.ExecuteReader()
' Bind the reader to the repeater.......................
blogRepeater.DataSource = reader
blogRepeater.DataBind()
' Close the reader
reader.Close()
' Close the connection
conn.Close()
Now I have a variable called My_Variable
. How can I place that variable My_Variable
inside my Repeater above?
现在我有一个名为My_Variable
. 如何将该变量My_Variable
放置在上面的中继器中?
回答by Maksim Kondratyuk
You need create protected method which return string for example:
您需要创建返回字符串的受保护方,例如:
protected string GetCustomString(object MyVariable)
{
retun string.Format("<a href='{0}'>{0}</a>", MyVaraible.ToString());
}
You can generate HTML link on this method. and in your repeater just bind it method in need place.
您可以在此方上生成 HTML 链接。并在您的中继器中将其绑定到需要的地方。
<asp:Repeater ID="blogRepeater" runat="server">
<ItemTemplate>
<%# GetCustomString(Eval("My_Variable")) %>
<asp:HyperLink ID="HyperLink2" runat=server NavigateUrl='<%#Eval("My_Variable", "CompanyProfile.aspx?ID={0}")%>'><%#Eval("Name")%></asp:HyperLink>
<br />
回答by Canavar
In this way you can bind your variable to repeater :
通过这种方式,您可以将变量绑定到 repeater :
<a href='<%= string.Format("CompanyProfile.aspx?ID={0}", My_Variable) %>'>
<%# Eval("Name") %>
</a>
EDIT :You're using static controls that doesn't need to be server controls. So you can use HTML elements and response.write for your global variable like that :
编辑:您使用的静态控件不需要是服务器控件。因此,您可以像这样对全局变量使用 HTML 元素和 response.write:
<img id="Image1" style="height:56px;width:64px;" src='<%= My_Variable %>' />
回答by zzzuperfly
you are not really have the db-code in codebehind right? this is just as an example? your site will be hacked within five minutes...
你不是真的有代码隐藏中的数据库代码吗?这只是一个例子?您的网站将在五分钟内被黑客入侵...
My_Variable have to be declared as a field not as a local variable in a function. Also databinding cannot see private fields.
My_Variable 必须声明为字段,而不是函数中的局部变量。数据绑定也看不到私有字段。
oh now I see you need to change the variable on every item right?
哦,现在我看到您需要更改每个项目的变量,对吗?
i suggest you create a dataobject to contain the data from db and set all relevant data on each object and then databind the repeater on the list. I realize I should show some code here but it is 2 years I wrote vb last so it would be wildly inaccurate anyway.
我建议您创建一个数据对象来包含来自 db 的数据,并在每个对象上设置所有相关数据,然后对列表中的中继器进行数据绑定。我意识到我应该在这里展示一些代码,但我上次写 vb 已经是 2 年了,所以无论如何它都会非常不准确。
回答by Phaedrus
To set the image url you must use a data binding expression. Also, you will need to make your variable 'My_Variable' a public class member.
要设置图像 url,您必须使用数据绑定表达式。此外,您需要将变量“My_Variable”设为公共类成员。
<asp:Repeater ID="blogRepeater" runat="server">
<ItemTemplate>
<br />
<asp:Image ID="Image1" runat="server" Height="56px" ImageUrl='<%# My_Variable %>' Width="64px" />
<asp:HyperLink ID="HyperLink2" runat=server NavigateUrl='<%#Eval("Company_ID", "CompanyProfile.aspx?ID={0}")%>'><%#Eval("Name")%></asp:HyperLink>
<br />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>