Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > asp.net สอบถามการทำ Radio button แบบ Group name และหา find control ของ gridview ครับ



 

asp.net สอบถามการทำ Radio button แบบ Group name และหา find control ของ gridview ครับ

 



Topic : 072022



โพสกระทู้ ( 24 )
บทความ ( 0 )



สถานะออฟไลน์




ตอนนี้ผมศึกษา find control ของ gridview อันนี้อยู่ครับ

https://www.thaicreate.com/asp.net/c-sharp-asp.net-gridview-control-findcontrol.html

อยากทราบตัวอย่างการทำแบบนี้เลยครับ แต่เปลี่ยนจาก check box เป็น radio button แบบ group name
ด้วยนะครับ คือเลือกได้เพียงอันเดียวจากในตารางครับ ไม่ทราบว่าควรทำอย่างไรครับ

โดยเฉพาะ code ส่วนนี้ครับว่าควรแก้อย่างไร ให้รับค่าจาก radion button

Code (C#)
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 = (CheckBox)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;
            }
        }


และการ group name ตอนนี้ลองทำเป็น group name แบบนี้ มันก็ไม่สามารถ group name แต่ละ
row ใน gridview ได้ครับ

Code (ASP)
	<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>




Tag : ASP.NET, Ms SQL Server 2008, C#, VS 2010 (.NET 4.x)







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-01-16 01:37:57 By : amuds View : 2699 Reply : 7
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

แค่เปลี่ยนจาก 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
 


 

No. 2



โพสกระทู้ ( 24 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 1 เขียนโดย : webmaster เมื่อวันที่ 2012-01-16 08:47:05
รายละเอียดของการตอบ ::
ผมลองเปลี่ยนแล้วครับ สิ่งที่เกิดขึ้นคือ group name radio button ไม่ได้ครับ
ทั้งๆที่ใส่ code group name ไปแล้วแบบนี้ครับ

Code (ASP)
<asp:TemplateField HeaderText="Select">
		<ItemTemplate>
            <asp:RadioButton id="chkCustomerID" GroupName="1" runat="server"></asp:RadioButton>
		</ItemTemplate>
	</asp:TemplateField>


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-16 13:43:51 By : amuds
 

 

No. 3



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

เอา Code ปัจจุบนขอบคุณมาดูครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-16 13:57:07 By : webmaster
 


 

No. 4



โพสกระทู้ ( 24 )
บทความ ( 0 )



สถานะออฟไลน์


อันนี้เป็น 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
 


 

No. 5



โพสกระทู้ ( 24 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 3 เขียนโดย : webmaster เมื่อวันที่ 2012-01-16 13:57:07
รายละเอียดของการตอบ ::
ตอนนี้ มัน คลิ๊ก radio button ได้หลายค่าอ่ะครับ ขอความกรุณาด้วยครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-16 15:56:09 By : amuds
 


 

No. 6



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

คุณน่าจะต้อง FindControl ที่ id มันครับ ทุกตัว
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-16 15:59:57 By : webmaster
 


 

No. 7



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ถ้าไม่อย่างงั้นคุณน่าจะต้องไปใช้ RadioButtonList แทนครับ

Go to : ASP.NET RadioButtonList and DataBinding
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-16 16:01:01 By : webmaster
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : asp.net สอบถามการทำ Radio button แบบ Group name และหา find control ของ gridview ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่