Click here to Skip to main content
16,015,900 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am building an asp.net website. I am new to this IT world.
I have a list view which contains a image and a label of the Category. I am wrapped these items in tag, so that whenever the user click in any of these controls it will redirect to another page. Outside the list view i have an ajax combo box which contains city names. It has a selected value on page load.
Now whenever a user clicks on either image or label inside list view it will redirect to page SubCat.aspx with query string values containing city and category name.
This query string will be passed in "a href="page?query string""

My problem is that i am not able to get the combo box value in a tag in aspx page. I know how to pass query string from code behind.

or is there anyway to put the values of querystring or form the query string in aspx.cs page and then retrieve it in "a" tag in aspx page?

Thank you.

code of aspx:

ASP.NET
<head>
<script>
    $( document ).ready(function() {
    var _cityId = $('#ComboBox1').val();
    var url = "http://google.com?cityid=" + _cityId;
    $( "span" ).on( "click", function() {
        window.location.href = url;
    });
});
</script>
</head>

<body>

 <asp:ComboBox ID="ComboBox1" runat="server" AutoCompleteMode="SuggestAppend" DataSourceID="SqlDataSource1"
                                            DataTextField="city" AutoPostBack="True" DataValueField="city" OnSelectedIndexChanged="ComboBox1_SelectedIndexChanged"
                                            MaxLength="0" Style="display: inline;">
                                            <asp:ListItem>---Select City---</asp:ListItem>
                                        </asp:ComboBox>

<asp:ListView ID="ListView1" GroupItemCount="3" GroupPlaceholderID="ContactRowContainer"
                                    ItemPlaceholderID="ContactItemContainer" runat="server">
                                    <LayoutTemplate>
                                        <table cellpadding="50" width="600px" runat="server" id="tblContacts">
                                            <tr id="Tr1" runat="server">
                                            </tr>
                                            <tr runat="server" id="ContactRowContainer" />
                                        </table>
                                        <br />
                                      
                                    </LayoutTemplate>
                                    <GroupTemplate>
                                        <tr runat="server" id="ContactRow">
                                            <td runat="server" id="ContactItemContainer" />
                                        </tr>
                                    </GroupTemplate>
                                    <ItemTemplate>
                                        <td runat="server" id="Td1" align="center">
                                         <%--   <a href="User/SubCat.aspx?cid=<%#Eval("cat_name") %>">--%>
                                         <span>
                                                <asp:Image ID="Image1" runat="server" Width="100" Height="100" ImageUrl='<%#Eval("image") %>' />
                                                <asp:Label ID="Label1" runat="server" Text='<%#Eval("cat_name") %>'></asp:Label></span>
                                        </td>
                                    </ItemTemplate>
                                </asp:ListView>

</body>
Posted
Updated 11-Aug-14 0:58am
v5
Comments
Trung Nguyen Son 11-Aug-14 6:57am    
I've cleared all other answers, which are not close enough to your question.
Check this below solution out!
Trung Nguyen Son 11-Aug-14 7:09am    
Please check out the solution 4.
I've made it success!

1 solution

Please follow this step:
1. Add this link in Head tag:
HTML
<script src="//code.jquery.com/jquery-1.10.2.js"></script>

2. Assume your HTML like this:
ASP.NET
<asp:dropdownlist id="ComboBox1" runat="server">
        <asp:listitem value="1">Hanoi</asp:listitem>
        <asp:listitem value="2">HCM City</asp:listitem>
    </asp:dropdownlist>

   <a href="User/SubCat.aspx?cid=wer">Redirect</a>

3. The script to get value in Combobox 1 then append param to href of the <a> tag below:
XML
<script>
        $(document).ready(function () {
            var _cityId = $('#<%= ComboBox1.ClientID %>').val();
            $("a").each(function () {
                $(this).prop('href', $(this).attr('href') + '&cityid=' + _cityId);
            });
        });
    </script>
 
Share this answer
 
v2
Comments
Ashi0891 11-Aug-14 8:01am    
I am afraid it didnt help me any :(
Trung Nguyen Son 11-Aug-14 9:11am    
There may be some missunderstandings!
I think you should post your full code (including aspx file and code behind) anh make clear the requirement. Then I will give it a try!
I find it not quite difficult to solve your problem!
Ashi0891 14-Aug-14 5:04am    
sorry I got busy with my other projects. Thank you for your solution.

replacing var _cityId = $('#<%= ComboBox1.ClientID %>').val();
with var _cityId = $("<%= ComboBox1.ClientID %>").val();
in your given code, solved my problem :)
Ashi0891 19-Aug-14 5:07am    
hey I am still not getting my required result. It doesnt show anything in cityid. Its empty.
Trung Nguyen Son 20-Aug-14 6:54am    
Pleas post your code, I will take a look!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900