Link to home
Start Free TrialLog in
Avatar of lz7cjc
lz7cjc

asked on

image button in datagrid

Hi
Sorry for the small number of points but it is all i have left and am waiting to get issues with premium services sorted...
in the meantime i would be very grateful if you could let me know how i would replace the following with a button that does the same thing:
<asp:HyperLinkColumn Text="Close Task" DataNavigateUrlField="itemid" DataNavigateUrlFormatString="close.aspx?itemid={0}&amp;url=index.aspx" HeaderText="Close Task"></asp:HyperLinkColumn>
               
i.e. reference closetask.jpg with the same datanavigateurlformatstring

thanks for your help
Nick
Avatar of lz7cjc
lz7cjc

ASKER

ok have got points issue sorted now...so have increased to fifty
Avatar of lz7cjc

ASKER

any thoughts? is this possible? thanks
Almost there...
Add:
<asp:TemplateColumn>
      <HeaderTemplate>
            Close Task
      </HeaderTemplate>
      <ItemTemplate>
      <asp:Button id="btnCloseTask" runat="server" Text="Close Task" onclick="btnCloseTask_Click"
            CommandArgument='<%#DataBinder.Eval(Container.DataItem, "itemid")%>'></asp:Button>
      </ItemTemplate>
</asp:TemplateColumn>

For VB.NET replace:
<%#DataBinder.Eval(Container.DataItem, "itemid")%>
with:
<%#Container.DataItem("itemid")%>

In your code behind, define btnCloseTask_Click (C#):

public void btnCloseTask_Click(object sender, System.EventArgs e)
{
      Button myButton = sender as Button;
      Response.Write("<script>alert(" + myButton.CommandArgument + ")</script>");
      //Redirect code goes here
}
Note, itemid will be in myButton.CommandArgument.
Avatar of lz7cjc

ASKER

Hi again!
bugger... so there is no easy way to do this? - will check it out and get back to you...
does this code sit within the datagrid?
thanks
Didn't think that was so bad... : )
Avatar of lz7cjc

ASKER

sorry ... been a long day and am not very good at this... but what is the code i need to put into the page if i am using VB?
so far i have:
<asp:TemplateColumn>
     <HeaderTemplate>
          Close Task
     </HeaderTemplate>
     <ItemTemplate>
     <asp:Button id="btnCloseTask" runat="server" Text="Close Task" onclick="btnCloseTask_Click"
          CommandArgument='<%#DataBinder.Eval(Container.DataItem, "itemid")%>'></asp:Button>
     </ItemTemplate>
</asp:TemplateColumn>
replacing
<asp:HyperLinkColumn Text="Close Task" DataNavigateUrlField="itemid" DataNavigateUrlFormatString="close.aspx?itemid={0}&amp;url=index.aspx" HeaderText="Close Task"></asp:HyperLinkColumn>
thanks
First,

For VB.NET replace:
<%#DataBinder.Eval(Container.DataItem, "itemid")%>
with:
<%#Container.DataItem("itemid")%>

And...

    Public Sub btnCloseTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
      System.Web.UI.WebControls.Button myButton = sender 'As  System.Web.UI.WebControls.Button;
      Response.Write("<script>alert(" + myButton.CommandArgument + ")</script>");
    End Sub

That's as close as I can get it....not sure if it'll work.  You see, sender needs to be cast as type 'Button' (where I commented out...)
Avatar of lz7cjc

ASKER

thnk i will pick this up in the morning.. thanks for all your help today
np,

Just try to get:

Button myButton = sender as Button;

converted to VB.NET
Avatar of lz7cjc

ASKER

I am afraid i can't get that to work
I was kind of hoping that i would be able to use something along the lines of:
        <asp:ImageButton ImageUrl="images/closetask.jpg"  runat="server" /> within the datagrid
where i could include
DataNavigateUrlField="itemid" DataNavigateUrlFormatString="close.aspx?itemid={0}&amp;url=index.aspx"
to give me the link text for the button

i.e it doesn't actually have to be a button; it can be an image source with a link; the key is that the hyperlink changes based on the row in the datagrid and the button repeats for each row
any ideas?
thanks
Well, with the above code that I gave you that's exactly what would happen.

The C# post above was working....I tested it.  I had a button in every row and when clicked the button, the row ID (or whatever other source value you specify) was displayed in the JS popup.

>I was kind of hoping that i would be able to use something along the lines of:
><asp:ImageButton ImageUrl="images/closetask.jpg"  runat="server" /> within the datagrid
>where i could include
>DataNavigateUrlField="itemid" DataNavigateUrlFormatString="close.aspx?itemid={0}&amp;url=index.aspx"
Yes, that would have been convenient, but unfortunately I don't think it's that easy : )
Avatar of lz7cjc

ASKER

ok will have another crack at this... but am not very good at VB let alone translating from C#
i will post the error and see if we can work through it together
lz7cjc, try this:

'put this in the aspx page:
<asp:TemplateColumn>
     <HeaderTemplate>
          Close Task
     </HeaderTemplate>
     <ItemTemplate>
     <asp:Button id="btnCloseTask" runat="server" Text="Close Task" onclick="btnCloseTask_Click"
          CommandArgument='<%#Container.DataItem("itemid")%>'></asp:Button>
     </ItemTemplate>
</asp:TemplateColumn>

'put this in the aspx.vb page:
Public Sub btnCloseTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
     Dim myButton As Button = CType(sender,Button)
     Response.Write("<script>alert(" + myButton.CommandArgument + ")</script>");
End Sub
Avatar of lz7cjc

ASKER

have done that:
BC30648: String constants must end with a double quote.
for this line
Response.Write("<script>alert(" + myButton.CommandArgument + ")</script>");
Firstly, in VB.NET no ';' at the end; sorry I missed that when posting!  Second, alert should have single quotes on the inside (although it would still work).   Just do this:

Replace:
Response.Write("<script>alert(" + myButton.CommandArgument + ")</script>");

with:
Response.Write("<script>alert('" + myButton.CommandArgument + "')</script>")
Avatar of lz7cjc

ASKER

tried that... still get
BC30648: String constants must end with a double quote.
Response.Write("<script>alert('" + myButton.CommandArgument + "')</script>")

also the "end sub" is the wrong colour... like it hasn't been recognised as the end of the function
ASKER CERTIFIED SOLUTION
Avatar of martie_11
martie_11
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lz7cjc

ASKER

fantastic... that is awesome... the button appears... but it doesn't then go to close.aspx?itemid={0}&amp;url=index.aspx
think we are almost there
You have to put that logic in the sub.  Put your re-direct statement in the sub and set itemid equal to myButton.CommandArgument
click on the button....itemid should print on the page when you do so
Avatar of lz7cjc

ASKER

yes - at the top of the page... just seen it
Avatar of lz7cjc

ASKER

think i have understood what you have done... so have substituted with:
Response.redirect("close.aspx?itemid=" & myButton.CommandArgument)
is that right?
Yep...should do it.
Avatar of lz7cjc

ASKER

fantastic... that looks like it is sorted... thanks once again for your help
all the best
Nick
Avatar of lz7cjc

ASKER

btw do i need to repeat the whole thing for multiple buttons?
If you want two, three... buttons...then yes.
Avatar of lz7cjc

ASKER

ok  thanks