Use the below code to get the value from the Gridview on selection of Row
dim a as string
dim b as string
a=GridView1.DataKeys(GridView1.SelectedIndex).Values("UserName").ToString()
b=GridView1.DataKeys(GridView1.SelectedIndex).Values("Surnmae").ToString()
The Value you are accessing from the gridview like for EX:UserName,Surname these you have write in the Gridview Tag --> DataKeyNames="UserName,Surname"
christopheEU
Member
51 Points
134 Posts
how to get value from gridview
Apr 10, 2008 07:34 AM|LINK
Hi,
I'm a beginner with ajax, i use visual studio 2005 and ajax is installed on my computer!
I have a gridview with a column select and on this column i'd like to put a link to a function javascript (alert)
for displaying some information about the row ?
Sample click on row 1 result : > alert('Your name is ' + UserName+ 'Your surname is : ' + Surnmae)
someone can help me to do that ?
Thanks for all
Christophe
ajax alert
Mulukutla
Member
275 Points
90 Posts
Re: how to get value from gridview
Apr 10, 2008 08:55 AM|LINK
Hi,
In gridview selected index changing event you get the current row index and values based on that,. and have a clientscript message for alert.
Regards,
Mulukutla[cool]
christopheEU
Member
51 Points
134 Posts
Re: how to get value from gridview
Apr 10, 2008 09:01 AM|LINK
Hi mulukulta,
Thanks but have you got a sample for me ?
Thanks
Christophe
Avinash Desa...
Contributor
2161 Points
721 Posts
Re: how to get value from gridview
Apr 10, 2008 09:16 AM|LINK
Hi
Use the below code to get the value from the Gridview on selection of Row
dim a as string dim b as string a=GridView1.DataKeys(GridView1.SelectedIndex).Values("UserName").ToString() b=GridView1.DataKeys(GridView1.SelectedIndex).Values("Surnmae").ToString()The Value you are accessing from the gridview like for EX:UserName,Surname these you have write in the Gridview Tag --> DataKeyNames="UserName,Surname"
~Avinash desai~
Software Developer
Bangalore
Please remember to click "Mark as Answer" on this post if it helped you.
vinz
All-Star
128493 Points
18150 Posts
MVP
Re: how to get value from gridview
Apr 10, 2008 09:24 AM|LINK
Hi christopheEU,
Please refer to this link below
http://forums.asp.net/p/1242770/2275892.aspx#2275892
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
christopheEU
Member
51 Points
134 Posts
Re: how to get value from gridview
Apr 10, 2008 09:34 AM|LINK
hi,
thanks all for your fast help !
that's work
Christophe
vinz
All-Star
128493 Points
18150 Posts
MVP
Re: how to get value from gridview
Apr 10, 2008 09:43 AM|LINK
Here's a working example
<script type="text/javascript" language="javascript">
function alertuser()
{
var user = document.getElementById('<%= HiddenField1.ClientID %>').value;
if (confirm("Your Name is "+ "" + User)==true)
return true;
else
return false;
}
</script>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField />
<asp:BoundField />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
RELEVANT CODES
private void bindDummy()
{
DataTable dt = new DataTable();
dt.Columns.Add("Val1");
dt.Columns.Add("Val2");
dt.Columns.Add("val3");
DataRow dr = dt.NewRow();
dr["Val1"] = "value1";
dr["Val2"] = "value2";
dr["Val3"] = "value3";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
HiddenField1.Value = User.Identity.Name.ToString();
if (!Page.IsPostBack)
{
bindDummy();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.SelectedIndex == i)
{
LinkButton lb = (LinkButton) GridView1.Rows[i].Cells[0].Controls[0];
if (lb != null)
{
lb.Attributes.Add("onclick", "javascript:return alertuser();");
}
}
}
}
I have tested on this and works fine..
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
christopheEU
Member
51 Points
134 Posts
Re: how to get value from gridview
Apr 10, 2008 11:17 AM|LINK
thanks you very much for your sample !
christophe