Hi nauna,
Check this example. Now please take its reference and correct your code.
SQL
CREATE TABLE tblFiles
(
Id INT PRIMARY KEY NOT NULL,
ImageName VARCHAR(100) NULL,
Path nvarchar(500) NULL
)
GO
INSERT INTO tblFiles VALUES(1,'Chrysanthemum.jpg','Images/Chrysanthemum.jpg')
INSERT INTO tblFiles VALUES(2,'Desert.jpg','Images/Desert.jpg')
INSERT INTO tblFiles VALUES(3,'Hydrangeas.jpg','Images/Hydrangeas.jpg')
INSERT INTO tblFiles VALUES(4,'Jellyfish.jpg','Images/Jellyfish.jpg')
INSERT INTO tblFiles VALUES(5,'Koala.jpg','Images/Koala.jpg')
INSERT INTO tblFiles VALUES(6,'Lighthouse.jpg','Images/Lighthouse.jpg')
INSERT INTO tblFiles VALUES(7,'Penguins.jpg','Images/Penguins.jpg')
INSERT INTO tblFiles VALUES(8,'Tulips.jpg','Images/Tulips.jpg')
HTML
<asp:RadioButtonList runat="server" ID="rblImages" RepeatColumns="3" RepeatLayout="Table"
RepeatDirection="Horizontal">
</asp:RadioButtonList>
<hr />
<asp:Button Text="Save" runat="server" OnClick="Save" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindRadioButtonList();
}
}
protected void Save(object sender, EventArgs e)
{
string value = rblImages.SelectedValue;
string name = GetImagesData().Select("Id=" + value)[0]["ImageName"].ToString();
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Selected Image\\nName : " + name
+ "\\nValue : " + value + "')", true);
}
private void BindRadioButtonList()
{
for (int i = 0; i < this.GetImagesData().Rows.Count; i++)
{
DataRow dr = GetImagesData().Rows[i];
ListItem item = new ListItem("<img src='" + dr["Path"] + "' alt='" + dr["ImageName"] + "'/>", dr["Id"].ToString());
rblImages.Items.Add(item);
}
}
private DataTable GetImagesData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM tblFiles";
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindRadioButtonList()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim value As String = rblImages.SelectedValue
Dim name As String = GetImagesData().Select("Id=" & value)(0)("ImageName").ToString()
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('Selected Image\nName : " & name _
& "\nValue : " & value & "')", True)
End Sub
Private Sub BindRadioButtonList()
For i As Integer = 0 To Me.GetImagesData().Rows.Count - 1
Dim dr As DataRow = GetImagesData().Rows(i)
Dim item As ListItem = New ListItem("<img src='" & dr("Path") & "' alt='" + dr("ImageName") & "'/>", dr("Id").ToString())
rblImages.Items.Add(item)
Next
End Sub
Private Function GetImagesData() As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM tblFiles"
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
cmd.Connection = con
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot