open of popup on click of gridview linkbutton - asp.net

I have Gridview and ,it consists of 3 columns , the 1st column is of ID,2nd of date and 3rd of description.
the 1st column ID's are all linkbutton and when i click the linkbutton a popup window has to open.
and the text of the linkbutton should be populated in the textbox of popup window. plz help me

Add a hyperlink column to your grid view, pass the text as query string to the pop up page, don't forget to set target property to _blank.
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="AnyText"
DataNavigateUrlFormatString="Page.aspx?text={0}"
DataTextField="AnyText" HeaderText="Test" Target="_blank" />
</Columns>
</asp:GridView>
On page.aspx you need to place a textbox then in page load event add:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = Request.QueryString["text"];
}
Hope this is what are you looking for.

Related

select data from gridview field and populate textbox

I'm trying to populate a single text box (or parameter) with data from a gridview column when I click on a button in that row.
Gridview gets it data from a sqlconnection
the gridview is
| Drawing |
| 12345 | VIEW
| 12346 | VIEW
the VIEW is a template button with an onclick event, when the user clicks the button the data from the Drawing column (12345) should be passed to ether a textbox or a paremeter. (this is the part I dont know how to do) once the Iv got the number in a textbox I can use it as pareameter and then a pdf is opened of that drawing, I have code for this and is working.
thanks for any help
If you are using C#, the simplest thing to do would be to add an in-built select command button to the gridview rows at runtime. Then on the selectedindexchanged event of the gridview simply access the cell of the selected row that you want the value from. You can then assign that string to anything you want. Like so:
protected void myGridView_SelectedIndexChanged(object sender, EventArgs e)
{
string myString = myGridView.SelectedRow.Cells[4].Text.ToString();
TextBox1.Text = myString;
}
Remember that the cell index collection is zero based, so [0] is actually the first cell in the row.
Use TemplateFields and the grid view's OnRowCommand event, like this:
Markup:
<asp:gridview id="GridView1"
OnRowCommand="GridView1_RowCommand"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBoxDrawing" runat="server"
Text="<%# Eval("Drawing")) %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="selc" runat="server" Text="View"
CommandName="View"
CommandArgument="<%# ((GridViewRow)Container).RowIndex %> />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked
if(e.CommandName == "View")
{
// Convert the row index stored in the CommandArgument
// property to an integer
var index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection
var row = GridView1.Rows[index];
// Find the drawing value
var theDrawingTextBox = row.FindControl("TextBoxDrawing") as TextBox;
// Verify the text box exists before we try to use it
if(theDrawingTextBox != null)
{
var theDrawingValue = theDrawingTextBox.Text;
// Do something here with drawing value
}
}
}

how to take value from grid view and stores into text box ,on a template button event?

I have created a form for saving Medium details, columns are, MID,MediumName,CreationDate,Status.I have bind full table in a gridview name gvmedium, i am having a textbox on the form .I want to fill the text box from grid views value of column MediumName on the template button click event.can any one help me?
You can use CommandName and Command Argument to achieve it and on Code Behind use RowCommand event.
<ItemTemplate>
<asp:LinkButton ID="lnkResetPassword" Text="Reset Password" runat="server" CommandName="ResetPassword" CommandArgument='<%# Bind("UserId") %>'></asp:LinkButton>
</ItemTemplate>
and then do your work here
protected void grdUserGroupList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ResetPassword")
{
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
lblMId.Text = e.CommandArgument.ToString();
txtMedium.Text=gvMedium.Rows[RowIndex ].Cells[0].Text;
}
}

How to redirect to two different pages in radgrid using two command buttons inside radgrid?

I have a radgrid1 and inside radgrid Item template i have two asp.net imagebutton namely imagebutton1 and imagebutton2 ....
i want when i click on Selected radgrid Item whose id is id then after clicking on Image Button1 i redirect to ~/book.aspx?id=1
and if i click on imagebutton2 then i redirect to ~/details.aspx?id=1
Note : the id of the item will be changed dynamically according to the selected radgrid row .
I have already done it using simple Gridview but m unable to perform this action using radgrid.
Help me please guys...!
Please check below code snippet.
.aspx
<MasterTableView DataKeyNames="Id"
>
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="BookPage"/>
<asp:ImageButton ID="ImageButton2" runat="server" CommandName="DetailPage"/>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
.asp.cs
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
GridDataItem item = e.Item as GridDataItem;
string strId = item.GetDataKeyValue("Id").ToString();
if (e.CommandName == "BookPage")
{
Response.Redirect("~/book.aspx?id=" + strId);
}
else if (e.CommandName == "DetailPage")
{
Response.Redirect("~/details.aspx?id=" + strId);
}
}

ASP.NET GridView Data which i set on RowDatabound event loses after post back

I have an asp.net page where i have a gridview control which bind data from a DataTable.
In my 5 th column of the grid i am showing a Radio button list which has 2 Radio button items (Yes or No). For Some rows i will not show the RadioButton control, if the 4 th column cell value is empty. It works fine.But in my button click event (postback), the Grid is showing the Radio button list for those cells which was not shown initially. I have enabled ViewState for page and Control
This is my code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=false
DataKeyNames="RequestDetailId" ondatabound="GridView1_DataBound" EnableViewState ="true" AllowPaging=false
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="RequestDetailId" HeaderText="Request Detail Id" />
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="NewOffer" HeaderText="New Offer" />
<asp:TemplateField HeaderText="Your Response" ItemStyle-CssClass="radioTD">
<ItemTemplate>
<asp:RadioButtonList ID="radioList" runat="server">
<asp:ListItem Text="Yes" Value="Accept"></asp:ListItem>
<asp:ListItem Text="No" Value="Reject"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
in code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadItemDetails();
}
}
private void LoadItemDetails()
{
DataTable objDt= GetGridDataSource();
if (objDt.Rows.Count > 0)
{
GridView1.DataSource = objDt;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(String.IsNullOrEmpty (e.Row.Cells[3].Text.Trim())||(e.Row.Cells [3].Text ==" "))
{
e.Row.Cells[4].Text = "";
}
}
My results are
Before Postback
After Postback
How do i maintain the content after postback ? Thanks
The problem is that you are setting the Text of the GridView table cell to an empty string rather than setting the visibility of the RadioButtonList control to false.
Clearing the Text property is removing the markup for the RadioButtonList on the first load, but on postback the RowDataBound event is not fired and the RadioButtonList control is recreated and displayed again.
To avoid this you could find the control and set its visibility to false, this will then be remembered across postbacks.
Try the following:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (String.IsNullOrEmpty(e.Row.Cells[3].Text.Trim()) || (e.Row.Cells[3].Text == " "))
{
RadioButtonList radioList = (RadioButtonList)e.Row.FindControl("radioList");
radioList.Visible = false;
}
}
Hope this helps.
you can do something like this ..
from the description of the problem. it sounds as if you are doing the databinding in the code behind. in such case asp.net does not preserve the datasource in the viewstate for you. try retrieving the data and storing it in the ViewState hashtable object with something like
ViewState["GridviewData"] = GridviewData
and retreiving it from there between postbacks

writing code in button's click event in gridview

i have added a button control in gridview and i want to redirect
to the another page on button's click event,
and also i want to access the values of selected row in a gridview and show that values on other page on button
click event which is inside the gridview,, can anybody plz help me out in this .
this should be helpful: http://msdn.microsoft.com/en-us/library/bb907626.aspx
for accessing values inside RowCommand event find your row first (by index):
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = MyGridView.Rows[index];
and then controls inside it which holds the values you need:
Label MyLabel= (Label)row.FindControl(MyLabel);
after that all you need to do is to transfer to your page and send values from your controls with it (do that with querystring):
Server.Transfer("MyPage.aspx?value="+MyLabel.Text)
here you can read more about gridview RowCommand event: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
You need to add either a ButtonField or a TemplateField with a button inside and then you can add your code to the RowCommand and use the e.CommandName and e.CommandArgument
To redirect you can use Response.Redirect(address)
To see the values it depends where in the program you want to do it. If during databinding you can use the dataitem("column"). Outside of databinding you can go through the Gridview.Rows collection for the row you want and then look at the row.Cells(columnNumber).Text to see the value.
we have an event in gridview called, gridview rowcommand event, in that you can write the code for redirecting to another page using button. First you have to bind the value of which you want to pass in button property called commandargument.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Response.Redirect("abc.aspx?id=" + e.CommandArgument);
//here id means passing the value using querystring to another page.
}
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Name" DataField="fullname" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button runat="server" ID="btnDelete" Text="Delete" OnClick="btnDelete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
// This focuses the current row where the button lies. you can perform all events of server-controlls
//instead of Button e.g LinkButton, Dropdownlist index changes...
protected void btnDelete_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvRow = (GridViewRow)btn.NamingContainer;
string id = gvRow.Cells[0].Text;
string name = gvRow.Cells[1].Text;
//use these values in query string or any logic you prefer for cross page posting
Response.Redirect("your URL ?id=" + id + "&name=" + name);
}

Resources