Question

in one of my pages I got following in CodeBehind to redirect to another page.

protected void btnEASYBRIEF_Click(object sender, EventArgs e)
        {
            Response.RedirectToRoutePermanent("../Prints/EASYBRIEF.aspx?" + grdFlights.SelectedDataKey.Value);
        }

I want to change it to a "onclientclick" event like

onclientclick="window.open('../Prints/EASYBRIEF.aspx?
                     +grdFlights.SelectedDataKey.Value')"

How would be the right text after ../Prints/EASYBRIEF.aspx

Thanks in advance

Was it helpful?

Solution

I would create a property to access like

Code Behind

string _selectedValue;


public string SelectedValue {

    get { return _selectedValue; }
}

Set the '_selectedValue' as your grdFlights.SelectedDataKey.Value

Then in the .aspx page you can do

var value = <%# SelectedValue%>;

onclientclick="window.open('../Prints/EASYBRIEF.aspx?' + value)"

Something to that effect.

OTHER TIPS

Each server side button have one Client Id as well. You just have to access that element with client Id and take its value

Try the following

<script type="text/javascript">
function HandleClick()
{
   window.open('../Prints/EASYBRIEF.aspx?'+
                     +document.getElementById("<%=grdFlights.ClientId%>").Value;
}
</script>

you can you inspire this part of code, it's almost the same requirement.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type = "text/javascript">
        function GetSelectedRow(lnk) {
            var row = lnk.parentNode.parentNode;
            var rowIndex = row.rowIndex - 1;
            var customerId = row.cells[0].innerHTML;
            var city = row.cells[1].getElementsByTagName("input")[0].value;
            alert("RowIndex: " + rowIndex + " CustomerId: " + customerId + " City:" + city);
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns = "false" AllowPaging = "true" OnPageIndexChanging = "PageIndexChanging">
        <Columns>
        <asp:BoundField DataField = "CustomerID" HeaderText = "CustomerID" />
        <asp:TemplateField HeaderText = "City">
            <ItemTemplate>
                <asp:TextBox ID="txtCity" runat="server" Text = '<%# Eval("City") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkSelect" runat="server" Text="Select" CommandName = "Select" OnClientClick = "return GetSelectedRow(this)" />
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>

be careful the line "return false;" at end javascript function it's very important

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top