TryHard
TryHard

Reputation: 33

How do i pass a link from selected query in a binded dropdownlist?

ASPX

<asp:DropDownList ID="DropDownList1" DataTextField="Description"    
    DataValueField="NID" runat="server">
</asp:DropDownList> 

C#

 string CS = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("Select Description, NID, Link from PushNotification", con);
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                DropDownList1.DataTextField = "Description";
                DropDownList1.DataValueField = "NID";
                DropDownList1.DataSource = rdr;
                DropDownList1.DataBind();
            }
        }

When a selected item is click in the dropdownlist it will redirect to the link from the selected query.

Upvotes: 0

Views: 49

Answers (1)

Sunil
Sunil

Reputation: 3424

I suggest you change you SQL query to: (assuming SQL Server)

Select Description, NID + '|' + Link NIDLink from PushNotification

Then, change DataValueField to:

DropDownList1.DataValueField = "NIDLink";

Finally, in your code:

string[] values = DropdownList1.SelectedValue.Split('|');
string nid = values[0];
string link = values[1];
Response.Redirect(link);

Upvotes: 1

Related Questions