Gagan Sikri

Gagan Sikri

  • NA
  • 1.6k
  • 222.5k

Drop Down List Data Binding Issue

Sep 26 2014 5:40 AM
Dear Sir/Madam
 
 I am facing an issue with dropdownlist. I am binding data in dropdownlist using linq query keeping both datatextfield and datavaluefield as different attributes in table. I have set autopostback false but still when user selects a value from dropdown it does not keep value and comes back to initial value. Please help me out.
 
CS Code:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class EventHost_Registrants : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["host"] == null)
{
Response.Redirect("~/Error.aspx");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoServerCaching();
HttpContext.Current.Response.Cache.SetNoStore();
}
else
{
Label2.Text = Session["host"].ToString();
detail();
counevents();
loadevents();
GridView1.Visible = false;
}
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("~/EventHost/Dashboard.aspx");
}
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
Session.RemoveAll();
Session.Abandon();
Response.Redirect("~/EventHost/AfterLogOut.aspx");
}
void detail()
{
eventmasterDataContext d = new eventmasterDataContext();
var query=from eventhost k in d.GetTable<eventhost>()
where k.username==Label2.Text
select k;
Label3.Text = query.SingleOrDefault().name;
Label4.Text = query.SingleOrDefault().organisation;
Label7.Text = Convert.ToString(query.SingleOrDefault().hostid);
}
void counevents()
{
eventmasterDataContext count = new eventmasterDataContext();
var query2 = from eventmaster m in count.GetTable<eventmaster>()
where Convert.ToString(m.hostid) == Label7.Text
select m;
int result = query2.Count();
Label6.Text = Convert.ToString(result);
}
void loadevents()
{
eventmasterDataContext load = new eventmasterDataContext();
var query = from eventmaster k in load.GetTable<eventmaster>()
where k.hostid == Int32.Parse(Label7.Text)
select k;
DropDownList1.DataSource = query;
DropDownList1.DataTextField = "eventname";
DropDownList1.DataValueField = "eventid";
DropDownList1.DataBind();
}
void regns()
{
eventmasterDataContext reg = new eventmasterDataContext();
var query = from regnmaster k in reg.GetTable<regnmaster>()
where Convert.ToString(k.eventid)==DropDownList1.SelectedValue
select k;
GridView1.Visible = true;
GridView1.DataSource = query;
GridView1.DataBind();
}
protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
{
regns();
}
}
 
Page Source:
 
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="Registrants.aspx.cs" Inherits="EventHost_Registrants" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.auto-style1 {
text-align: right;
}
.newStyle1 {
font-family: "Segoe UI";
font-size: large;
font-weight: bolder;
font-style: inherit;
color: #E45F56;
}
.auto-style2 {
}
.auto-style3 {
text-align: right;
width: 589px;
}
.newStyle2 {
font-family: "Segoe UI";
font-size: large;
font-weight: bolder;
font-style: inherit;
color: #35404F;
border: thin solid #E45F56;
}
.newStyle3 {
font-family: "Segoe UI";
font-size: large;
}
.auto-style5 {
font-family: "Segoe UI";
font-size: large;
font-weight: bolder;
color: #35404F;
}
.auto-style6 {
text-align: center;
}
.auto-style7 {
height: 23px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style="width:100%;">
<tr>
<td class="auto-style1" colspan="2">
<asp:ImageButton ID="ImageButton2" runat="server" ImageAlign="Middle" ImageUrl="~/Files/Images/backdashpages.png" OnClick="ImageButton2_Click" />
&nbsp;&nbsp;
<asp:ImageButton ID="ImageButton3" runat="server" ImageAlign="Middle" ImageUrl="~/Files/Images/logoutotherpages.png" OnClick="ImageButton3_Click" />
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td class="auto-style1">
<asp:Label ID="Label7" runat="server" Text="Label" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style7"></td>
<td class="auto-style7"></td>
</tr>
<tr>
<td colspan="2">
<center><asp:Image ID="Image4" runat="server" ImageUrl="~/Files/Images/viewregnsbanner.png" /></center>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style6" colspan="2"><span class="auto-style5">USERNAME: </span>
<asp:Label ID="Label2" runat="server" CssClass="auto-style5" Text="Label"></asp:Label>
<span class="auto-style5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NAME: </span>
<asp:Label ID="Label3" runat="server" CssClass="auto-style5" Text="Label"></asp:Label>
<span class="auto-style5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORGANISATION: </span>
<asp:Label ID="Label4" runat="server" CssClass="auto-style5" Text="Label"></asp:Label>
<span class="auto-style5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NO. OF EVENTS: </span>
<asp:Label ID="Label6" runat="server" CssClass="auto-style5" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style3"><span class="newStyle1">Select Event:</span></td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="newStyle2" ForeColor="#E45F56" Height="33px" Width="273px">
</asp:DropDownList>
&nbsp;
</td>
</tr>
<tr>
<td class="auto-style2" colspan="2">
<center><asp:ImageButton ID="ImageButton4" runat="server" ImageAlign="Middle" ImageUrl="~/Files/Images/display.png" OnClick="ImageButton4_Click" /></center>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="auto-style2" colspan="2">
<center><asp:GridView ID="GridView1" runat="server" style="text-align: center" BackColor="White" BorderColor="#3F4061" BorderStyle="Solid" BorderWidth="2px" CellPadding="3" ForeColor="#35404F" GridLines="Vertical" AutoGenerateColumns="False" CssClass="newStyle3" AllowPaging="True" AllowSorting="True" EnableSortingAndPagingCallbacks="True">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="name" HeaderText="Attendee Name" SortExpression="name" />
<asp:BoundField DataField="emailid" HeaderText="Email ID" SortExpression="emailid" />
<asp:BoundField DataField="mobile" HeaderText="Mobile No." SortExpression="mobile" />
<asp:BoundField DataField="regnid" HeaderText="Registration ID" SortExpression="regnid" />
<asp:BoundField DataField="regntimestamp" HeaderText="Time Stamp" SortExpression="regntimestamp" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="#E45F56" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView></center>
</td>
</tr>
<tr>
<td class="auto-style2">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</asp:Content>
Please help me out. 

Answers (2)