asp.net สอบถามการทำ Radio button แบบ Group name และหา find control ของ gridview ครับ
แค่เปลี่ยนจาก CheckBox เป็น RadionButton ครับ
Code (C#)
void Button1_Click(object sender,EventArgs e)
{
RadioButton chkCusID;
Label lblID;
int i;
lblText.Text = "";
for( i = 0; i <= myGridView.Rows.Count - 1; i++)
{
chkCusID = (RadioButton)myGridView.Rows[i].FindControl("chkCustomerID");
lblID = (Label)myGridView.Rows[i].FindControl("lblCustomerID");
if(chkCusID.Checked)
{
//*** Have lblID.Text ***//
this.lblText.Text = this.lblText.Text + "<br>" + lblID.Text;
}
}
Date :
2012-01-16 08:47:05
By :
webmaster
เอา Code ปัจจุบนขอบคุณมาดูครับ
Date :
2012-01-16 13:57:07
By :
webmaster
อันนี้เป็น asp.net ครับ
Code (ASP)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="selectPan.aspx.cs" Inherits="MedicalWeb.selectPan" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>ThaiCreate.Com ASP.NET - GridView</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView id="myGridView" runat="server" AutoGenerateColumns="False" onRowDataBound="myGridView_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<%--<asp:CheckBox id="chkCustomerID" runat="server"></asp:CheckBox>--%>
<asp:RadioButton id="chkCustomerID" GroupName="1" runat="server"></asp:RadioButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CustomerID">
<ItemTemplate>
<asp:Label id="lblCustomerID" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label id="lblName" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label id="lblEmail" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CountryCode">
<ItemTemplate>
<asp:Label id="lblCountryCode" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Budget">
<ItemTemplate>
<asp:Label id="lblBudget" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Used">
<ItemTemplate>
<asp:Label id="lblUsed" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>
<hr />
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
Code (C#)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Xml;
using System.Collections.Generic;
using System.Net;
using System.IO;
namespace MedicalWeb
{
public partial class selectPan : System.Web.UI.Page
{
SqlConnection objConn;
SqlCommand objCmd;
string u;
private string connectionString = WebConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write("Name = " + Session["Username"] + "<br><br>");
string user = Session["Username"].ToString();
u = user;
string type = Session["Type"].ToString();
string name = Session["Name"].ToString();
string surname = Session["Surname"].ToString();
string mail = Session["Email"].ToString();
objConn = new SqlConnection(connectionString);
objConn.Open();
if (!Page.IsPostBack)
{
BindData();
}
//BindData();
}
void BindData()
{
String strSQL;
strSQL = "SELECT PanId, Name, Surname,Sex,Age,Inhospital FROM PanInfo WHERE Username = '" + u + "' ";
SqlDataReader dtReader;
objCmd = new SqlCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to GridView ***//
myGridView.DataSource = dtReader;
myGridView.DataBind();
dtReader.Close();
dtReader = null;
}
protected void Page_Unload(object sender, System.EventArgs e)
{
objConn.Close();
objConn = null;
}
protected void myGridView_RowDataBound(Object s, GridViewRowEventArgs e)
{
//*** CustomerID ***//
Label lblCustomerID = (Label)(e.Row.FindControl("lblCustomerID"));
if (lblCustomerID != null)
{
lblCustomerID.Text = DataBinder.Eval(e.Row.DataItem, "PanId").ToString();
//lblCustomerID.Text = (string)DataBinder.Eval(e2.Row.DataItem, "PanId");
}
//*** Email ***//
Label lblName = (Label)(e.Row.FindControl("lblName"));
if (lblName != null)
{
lblName.Text = (string)DataBinder.Eval(e.Row.DataItem, "Name");
}
//*** Name ***//
Label lblEmail = (Label)(e.Row.FindControl("lblEmail"));
if (lblEmail != null)
{
lblEmail.Text = (string)DataBinder.Eval(e.Row.DataItem, "Surname");
}
//*** CountryCode ***//
Label lblCountryCode = (Label)(e.Row.FindControl("lblCountryCode"));
if (lblCountryCode != null)
{
lblCountryCode.Text = (string)DataBinder.Eval(e.Row.DataItem, "Sex");
}
//*** Budget ***//
Label lblBudget = (Label)(e.Row.FindControl("lblBudget"));
if (lblBudget != null)
{
//lblBudget.Text = DataBinder.Eval(e.Row.DataItem, "Budget").ToString();
lblBudget.Text = (string)DataBinder.Eval(e.Row.DataItem, "Age");
}
//*** Used ***//
Label lblUsed = (Label)(e.Row.FindControl("lblUsed"));
if (lblUsed != null)
{
//lblUsed.Text = DataBinder.Eval(e.Row.DataItem, "Used").ToString();
lblUsed.Text = (string)DataBinder.Eval(e.Row.DataItem, "Inhospital");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CheckBox chkCusID;
Label lblID;
int i;
lblText.Text = "";
for (i = 0; i <= myGridView.Rows.Count - 1; i++)
{
chkCusID = (RadioButton)myGridView.Rows[i].FindControl("chkCustomerID");
lblID = (Label)myGridView.Rows[i].FindControl("lblCustomerID");
if (chkCusID.Checked)
{
//*** Have lblID.Text ***//
this.lblText.Text = this.lblText.Text + "<br>" + lblID.Text;
}
}
}
}
}
Date :
2012-01-16 14:12:15
By :
amuds
คุณน่าจะต้อง FindControl ที่ id มันครับ ทุกตัว
Date :
2012-01-16 15:59:57
By :
webmaster
Load balance : Server 04