Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing DataList buttons...

Status
Not open for further replies.

song2siren

Programmer
Jun 4, 2003
103
GB
Hello

I'm having difficulty displaying a particular image button in a DataList depending on the value of a field in my SQL database. I'm using the following to execute a stored procedure and retrieve a dataset:

Sub getResultsbySubject(Source as Object, e as EventArgs)
myDataAdapter = New SqlDataAdapter("Pubs_SearchbySubject", myConnection)
myDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
myDataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Search", SqlDbType.NVarChar, 255))
myDataAdapter.SelectCommand.Parameters("@Search").Value = frmSubjects.selectedvalue
resultDS = New DataSet()
myDataAdapter.Fill(resultDS, "Results")
dlResults.DataSource = resultDS.Tables("Results").DefaultView
Session("resultDSDataTable") = resultDS.Tables("Results")
BindDataList
myDataAdapter.Dispose()
myConnection.Close
End Sub

Sub BindDataList
dlResults.DataBind()
' Display a message if no results are found
If dlResults.Items.Count = 0 Then
ErrorMsg.Text = "No items matched your query."
else
ErrorMsg.Text = ""
End If
End Sub

If an InStock field in my database has a bit value of '1' I need to make an 'Add to Cart' image button display, whereas if the value is '0', I need to show an 'Out of Stock' image button. I'm not sure where to do this - maybe in my BindDataList sub? Anway, I'm sure this is not difficult, but any suggestions would be very much appreciated.

Thanks
 
I'd say, in your code-behind, add a method like this:

Code:
protected string GetCartImageUrl(bool isInStock)
{
    return(isInStock ? "AddToCart.gif" : "OutOfStock.gif");
}

And in your datagrid, have a template column for your image button:

Code:
<asp:TemplateColumn>
<itemtemplate>
<asp:ImageButton runat=server ImageUrl='<%# GetCartImageUrl(((int)DataBinder.Eval(Container,"DataItem.InStock")) == 1) %>' />
</itemtemplate>
</asp:TemplateColumn>

You might have to change the typecasted int to a byte... Can't remember off the top of my head and I don't have time to test it.

Hope that helps =D

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Thanks AtomicChip

Unfortunately this isn't working - I don't really know much about methods, but I get a 'Keyword is not valid as an identifier' error on 'protected string GetCartImageUrl(bool isInStock)' in my code behind file. Is this code method written in VB?

Apologies if I'm missing something really obvious.
 
Where exactly is the error occuring?

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Hi

The problem is with the method in the code behind file ie.

protected string GetCartImageUrl(bool isInStock)
{
return(isInStock ? "AddToCart.gif" : "OutOfStock.gif");
}

This causes the error message 'Keyword is not valid as an identifier'.

Thanks again.
 
Ooooohhhh...

You have to change the code that I've given you over to VB.NET code. Sorry, I'm a c# developer, not vb.

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Yeah, thought it looked odd. Had a stab at this, but without success.

Thanks anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top