C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Retriving selected value from a Datalist


Posted Date: 01 Oct 2008    Resource Type: Code Snippets    Category: ADO.NET

Posted By: ravi       Member Level: Gold
Rating:     Points: 10






Retriving selected value from a Datalist:



Html code:


<table width="100%">
<tr>
<td>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal"
OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<asp:RadioButton runat="server" ID="rbtn" GroupName="testGroup" />
</ItemTemplate>
</asp:DataList>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Show" runat="server" Text="show" OnClick="Show_Click" /> <asp:Label
ID="lblSelectedNo" runat="server"></asp:Label></td>
</tr>
<tr>
<td height="5">
</td>
</tr>
</table>



.CS file:

declarations :

private static SqlConnection con;
private static SqlCommand cmd;
private static DataTable dt = null;


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

dt = Loaddepartments(); // function to fill datalist from a table
DataList1.DataSource = dt;
DataList1.DataBind();


}
}

private DataTable Loaddepartments()
{

setConnection();

string strsql = "select eno from emp";

DataSet objDs = new DataSet();
SqlDataAdapter objDa = new SqlDataAdapter(strsql, con);
objDa.Fill(objDs, "DATA_TABLE");
objDa.Dispose();
return objDs.Tables["DATA_TABLE"];
}






private void setConnection()
{
con = new SqlConnection();
if (con.State == ConnectionState.Closed)
{
con = new SqlConnection(ConfigurationManager.AppSettings["connection string"].ToString());
con.Open();
}
}



protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
RadioButton rbtnReplyNos = (RadioButton)e.Item.FindControl("rbtn");
if (e.Item.ItemIndex > -1)
{

int iRow = e.Item.ItemIndex;
rbtnReplyNos.Text = dt.Rows[iRow].ItemArray[0].ToString();


}
string script = "SetUniqueRadioButton('DataList1.*testGroup',this)"; // javaScript for single selection of radiobuttons in datalist
rbtnReplyNos.Attributes.Add("onclick", script);
}



Javascript for single selection:


function SetUniqueRadioButton(nameregex, current)
{
re = new RegExp(nameregex);
for(i = 0; i < document.forms[0].elements.length; i++)
{
elm = document.forms[0].elements[i]
if (elm.type == 'radio')
{
if (re.test(elm.name))
{
elm.checked = false;
}
}
}
current.checked = true;
}




protected void Show_Click(object sender, EventArgs e)
{
foreach (DataListItem replyNos in DataList1.Items)
{
RadioButton rb = (RadioButton)replyNos.FindControl("rbtn");
rb.ID = replyNos.ToString();
bool resposse = rb.Checked;
if (resposse == true)
{
lblSelectedNo.Text = rb.Text;
DataList1.DataSource = dt;
DataList1.DataBind();
return;
}
}
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Retriving selected value from a Datalist  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Populate a dataset from datatable or collection or string values
Previous Resource: Saving Image in Sql Server
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

audio conferencing services

Contact Us    Privacy Policy    Terms Of Use