linkbutton dynamic image in itemtemplate

J

Jeronimo Bertran

Hi,

I am trying to display a column with a linkbutton with a bitmap which
depends on different criteria.

I currently have the following

<itemtemplate>
<asp:linkbutton runat="server" commandname="Respond"
Text="<img src='images/button1.gif'>" ></asp:linkbutton>
</itemtemplate>

Which works fine for fixed images... I now want to decide on the actual
file depending on a formula applied to a field name. I tried the
following:


Text="<img src='<%# GetProperFile(1)%>'>"

And defined GetProperFile in my code behind as:

public void GetProperFile(int nStatus)
{
Response.Write("images/button1.gif");
}

But it doesn't work.

Also, if I try to use the DataBinder as a parameter:

Text="<img src='<%# GetProperFile((int) DataBinder.Eval(Container.DataItem,
"status"))%>'>">

And try to swith to design view, I get a message Quote Values differently
inside a '<%..."value"...%>' block.

Thanks,

Jeronimo
 
J

Jeffrey Tan[MSFT]

Hi,

Thanks for posting in this group.
Based on my understanding, you want to change the text property at runtime.
If you want to dynamic determine the text property through databinding, I
think you can do like this:
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" Text="<%#GetImageHtml()%>" CommandName=""
CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

protected string GetImageHtml()
{
return "<img src='cut.ico' border=none>";
}

Also, you can do this not at databinding time, for example, you can do it
at button's click event:
private void Button1_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem item in DataGrid1.Items)
{

if(item.ItemType==ListItemType.Item||item.ItemType==ListItemType.Alternating
Item)
{
foreach(Control c in item.Cells[0].Controls)
{
if(c is System.Web.UI.WebControls.LinkButton)
{
LinkButton l=(LinkButton)c;
l.Text="<img src='cut.ico' border=none>";
}
}
}
}
}

If I misunderstand you, please feel free to tell me.
These 2 approaches both work well on my machine.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeronimo Bertran

Jeffrey,

Thanks for your response... What you describe below is more or less what
I need except that the actual icon used depends on the value of one of
the fields from the datasource. So, continuing with your example:

protected string GetImageHtml(int nStatus)
{
string filename = (nStatus == 1) ? "cut.ico" : "paste.ico";

string result;
result.format("<img src='%s' border=none", filename);
return result;
}


My problem now is providing the parameter based on a field value of the
current record. I tried the following:

<asp:LinkButton runat="server" Text="<%#GetImageHtml((int)
DataBinder.Eval(Container.DataItem, "Status"))%>" CommandName=""
CausesValidation="false"></asp:LinkButton>


But there is a problem with the quotes around the fieldmane.

Thanks again,

Jeronimo



(e-mail address removed) ("Jeffrey Tan[MSFT]") wrote in
 
J

Jeffrey Tan[MSFT]

Hi Jeronimo,

Thanks for your feedback.
I am glad that my reply makes sense to you.
For your following problem, I think there are 2 things you should be
careful of:
1). In your code: Text="<%#GetImageHtml((int)
DataBinder.Eval(Container.DataItem, "Status"))%>" , "Status" should be
'Status', because you already use " quote your <%#%>.
2). For your (int)DataBinder.Eval(Container.DataItem, "Status"), I think
you should be aware of the type of get object returned. If it is not a type
of System.Int32, then a runtime error will generate. I have ever met this
problem, because in my code, DataBinder.Eval(Container.DataItem,
"fieldname")'s type is System.Int16 instead of System.Int32.
At this time, I should use
(System.Int16)DataBinder.Eval(Container.DataItem, "fieldname"),

I hope this help you. If it still does not work, please feel free to let me
konw. I will work with you.

Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeronimo Bertran

Jeffrey,

The single quote for the fieldname is not working for me....I get an error
on line 33


Compiler Error Message: CS1012: Too many characters in character literal

Source Error:



Line 32: <ItemTemplate>
Line 33: <asp:LinkButton id=Linkbutton1 runat="server"
CausesValidation="false" Text="<%#GetImageHtml((System.Int16)
DataBinder.Eval(Container.DataItem, 'Flags'))%>" commandname="Respond">
</asp:LinkButton>
Line 34:
Line 35: </ItemTemplate>


Thanks again,

Jeronimo
 
J

Jeronimo Bertran

Jeffrey...

I figured it out... the single quote is used for the html code.

Thanks again.

Jeronimo
 
J

Jeffrey Tan[MSFT]

Hi Jeronimo,

Thanks for your feedback.
I am glad that you figured it out. Sorry, I pasted the wrong quotation mark
usage. The correct usage should be this:
Text='<%#GetImageHtml((System.Int16)DataBinder.Eval(Container.DataItem,"job_
id"))%>'
Because the DataBinder.Eval() method take 2 parameter, which the second
parameter is a string type. The single quoted content will be treated as
character instead of string. But for character, the "job_id" has too many
characters for ONE character, so "Compiler Error Message: CS1012: Too many
characters in character literal" will generate. If you replace "job_id" as
"j":
Text='<%#GetImageHtml((System.Int16)DataBinder.Eval(Container.DataItem,'j'))
%>' , you will find another error: The best overloaded method match for
'System.Web.UI.DataBinder.Eval(object, string)' has some invalid arguments.
Because the 'j' is suitable for single character, but it is not suitable
for the string parameter(DataBinder.Eval's second parameter)

So we should use "job_id" for DataBinder.Eval's second parameter. And for
not confusion compiler, we should use single quote for Text property, that
is why we should use:
Text='<%#GetImageHtml((System.Int16)DataBinder.Eval(Container.DataItem,"job_
id"))%>'

Anyway, congratulations to you for figuring it out !
I hope my input really helps you.

Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top