Link to home
Start Free TrialLog in
Avatar of csetzkorn
csetzkorn

asked on

navigation button in datagrid

Dear all,

At the moment I am using something like this within a datagrid:

<asp:HyperLinkColumn DataNavigateUrlField="faq_uid" DataNavigateUrlFormatString="faq_details.aspx?faq_uid={0}" DataTextField="question" HeaderText="Question"> <HeaderStyle Font-Bold="True"></HeaderStyle> </asp:HyperLinkColumn>

to enable the navigation to the details of an item. I would like to implement the same functionality with a button that has written on it: “More Details”. Any pointers would be very much appreciated. Many thanks in advance.

Chris
Avatar of mmarinov
mmarinov

Hi csetzkorn,

do you want to see this faq_details in a new window or you wanted to see them within the grid as nested datagrid ?

Regards!
B..M
Avatar of csetzkorn

ASKER

I would like to see them in a new (browser!) window. Thanks.
SOLUTION
Avatar of mmarinov
mmarinov

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
Thanks for this. Is there a better way? (without requiring javascript to be enabled in the client browser)
csetzkorn,
opening a new window is a javascript operation ( client script operation ) not server-side
B..M
Exactly. it is a 'client script operation' and I do not necessarily want the client to have JavaScript switched on.
csetzkorn,

so you can use normal <a> tag then with target attribute and this will open the new window
in this case you have to use asp:templatecolumn instead of HypeLinkColumn

B..M
use Target="_blank

<asp:HyperLinkColumn DataNavigateUrlField="faq_uid" DataNavigateUrlFormatString="faq_details.aspx?faq_uid={0}" DataTextField="question" HeaderText="Question"> <HeaderStyle Font-Bold="True" Target="_blank></HeaderStyle> </asp:HyperLinkColumn>

good luck
vardium,
cool, i didn't noticed that Target attribute is accessible to the HyperLinkColumn
this is it csetzkorn
B..M
sometimes i do the same thing. try to find a hard way, dont notice the easy way :-)
sorry i missed the last quotation
should be --> Target="_blank"
Just tried it. The Target attribute is NOT! accessible to the HyperLinkColumn.

BTW I would like to use a button rahter than a hyperlink (see original question). The latter works fine ...
csetzkorn,

what do you mean that is not accessible? this line is taken from MSDN:
            <asp:HyperLinkColumn
                 HeaderText="Select an Item"
                 DataNavigateUrlField="IntegerValue"
                 DataNavigateUrlFormatString="detailspage.aspx?id={0}"
                 DataTextField="PriceValue"
                 DataTextFormatString="{0:c}"
                 Target="_blank"/>

also if you want to use button i'm afraid you have to use javascritp :(

B..M
yes i think so.
there is no way to do this without javascipt if you want a button.
This works:

 <asp:HyperLinkColumn
                 HeaderText="Select an Item"
                 DataNavigateUrlField="IntegerValue"
                 DataNavigateUrlFormatString="detailspage.aspx?id={0}"
                 DataTextField="PriceValue"
                 DataTextFormatString="{0:c}"
                 Target="_blank"/>

but your old proposal didn't work:

<asp:HyperLinkColumn DataNavigateUrlField="faq_uid" DataNavigateUrlFormatString="faq_details.aspx?faq_uid={0}" DataTextField="question" HeaderText="Question"> <HeaderStyle Font-Bold="True" Target="_blank"></HeaderStyle> </asp:HyperLinkColumn>

anyway, I guess I have to assume that the user uses javascript ...




I think some confusion has arisen here. My original code works:

<asp:HyperLinkColumn DataNavigateUrlField="faq_uid" DataNavigateUrlFormatString="faq_details.aspx?faq_uid={0}" DataTextField="question" HeaderText="Question"> <HeaderStyle Font-Bold="True"></HeaderStyle> </asp:HyperLinkColumn>

However, I would like to replace the link with a button with the text ‘More Details’.

Thanks again.
csetzkorn,

the you have to use asp:TemplateColumn
create ItemDataBound event and add the onclick event to it to open the javascript

btn.Attributes.Add("onclick", "window.open('faq_details.aspx?faq_uid=" + your id column value + "'_); return false;" );

B..M
I have increased the number of points. A bit more code would be very much appreciated. Many thanks!
csetzkorn,

check this:

<asp:TemplateColumn>
    <asp:Label id="faqUid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "faq_uid") %>'></asp:Label>
    <asp:Button id="btnMoreDetails" Text="MoreDetails" runat="server"></asp:Button>
</asp:TemplateColumn>

create ItemDataound event for the datagrid and in it write down

if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
{
    Button btn = (Button)e.Item.FindControl("btnMoreDetails");
    Label lbl = (Label)e.Item.FindControl("faqUid");
    btn.Attributes.Add ( "onclick", "window.open('faq_details.aspx?faq_uid=" + lbl.Text+ "'); return false;" );
}

B..M
it should be :

<asp:HyperLinkColumn DataNavigateUrlField="faq_uid" DataNavigateUrlFormatString="faq_details.aspx?faq_uid={0}" DataTextField="question" HeaderText="Question" Target="_blank"><HeaderStyle Font-Bold="True"></HeaderStyle>
</asp:HyperLinkColumn>

try this
mmarinov,

When I try your code, I get:

Type 'System.Web.UI.WebControls.TemplateColumn' does not have a property named 'asp:Label'
csetzkorn,

I'm really sorry ( on of that bad days :( )

put the asp:Label and asp:button in

<asp:TemplateColumn>
    <itemTemplate>
        here
    </itemTemplate>
</asp:TemplateColumn>

B..M
Ok, I have this:

aspx file:

<asp:TemplateColumn>
<itemTemplate>
<asp:Label id="faqUid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "faq_uid") %>'></asp:Label>
<asp:Button id="more_details" Text="More Details" runat="server"></asp:Button>
</itemTemplate>
</asp:TemplateColumn>

code behind file:

public void Item_Bound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button btn = (Button) e.Item.FindControl("more_details");
Label lbl = (Label) e.Item.FindControl("faqUid");
btn.Attributes.Add ( "onclick", "window.open('faq_details.aspx?faq_uid=" + lbl.Text+ "'); return false;" );
}
}

Unfortunately it doesn't work. I also don't understand what the ItemDataBound event has to do with my problem. As far as I understand it allows you to access the data before the item is displayed in the control.

Thanks.
csetzkorn,

the code above add a client script to your button before it is displayed on the page
so when you click on the button to open a new window

what is the result of the code above ?

B..M
did you try my code?
ASKER CERTIFIED SOLUTION
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
vardium,

Yes, your code works but I already got this working with my code above (see original post). the diffrence is that you open a new window. Thanks anyway.

Vardium,

Your code:

<asp:TemplateColumn>
<itemTemplate>
<input onclick="window.open('faq_details.aspx?faq_uid=<%# DataBinder.Eval(Container.DataItem, "faq_uid") %>'); return false;" name="more_details" id="more_details" type="submit" value="More Details" />
</itemTemplate>
</asp:TemplateColumn>

does what I want. Many thanks.
great varium, i haven't think of input control
but csetzkorn, i'm just curious - is the code i've posted didn't act the same ?

B..M
mmarinov,

The code you have given me (the one I have posted) didn't thorw any errors but didn't do anything.

Thanks a lot for your help.