asp.net gridview onrowcommand -->( GridViewCommandEventArgs) - c#

I want to ask how to get a text or value of a button inside the gridview?
but i want to get the text value from this onrowcommand which use GridViewCommandEventArgs as its parameter.
as if im using onrowdatabound is (GridViewRowEventArgs), which makes it easy for me to get the button.text inside the gridview
string example = ((Button)e.Row.FindControl("btnStop")).Text;
I want to get the button.text to do an if else loop inside the onrowcommand.
Anyone know how?
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="2" CellSpacing="2" HorizontalAlign="Center" PageSize="5" Width="133%" DataKeyNames="SurveyID" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" onrowcommand="stop_survey"
onrowdatabound="filter_select" onselectedindexchanging="selected"
>
code behind
public void filter_select(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{....
((Button)e.Row.FindControl("btnStop")).Text = "Start";
}
}
public void stop_survey(object sender, GridViewCommandEventArgs e)
{
//i want to get the "btnStop" button text which is nested on the gridview.
}
i want to get the btnStop text, as I want to have different sqlstatement depending on its text (eg. Start or Stop)
the problem is i cant do e.Row inside stop_survey.
Please guide me.

Create a object of GridViewRow from refrence of command source row.
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
But keep in mind that which type of object control is you are using to call event in above code is LinkButton. But if You Calling Rowcommand event from Simple Button then You need to Write this:
GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
After Creating Row Object You can simply find any control from Gridview using rowindex.
Label LabelEmail = (Label)GridView1.Rows[row.RowIndex].FindControl("LabelEmail");
Finally You Can Access that Properties of that Control.
LabelResult.Text = LabelEmail.Text;
Same as Above You can also find Datakeys of gridview using GridViewRow rowindex.
int code = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0].ToString());

Related

Gridview on Linkbutton click, Pass complete GridRow information to the next page? What are best ways to do this?

I have Gridview that has 6 other BoundField Columns and one Linkbutton column.
I want to pass all the Row information to the next page when users click on LinkButton.
What are the best ways to do this?
If we pass it as QueryString, it is going to be Too long.
Thanks
You can use the LinkButton Column itself to your advantage.
The idea is to send the RowIndex of the GridViewRow in PostBackUrl property of LinkButton as a QueryString Parameter.
Use the Container.DataItemIndex which represents the Index of the Row.
At the Next/Destination page , get the RowIndex from QueryString to get the GridViewRow and then read the Column values.
Step 1.) The markup corresponding to LinkButton should be as below:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDetails" runat="server" Text="Send Details"
PostBackUrl='<%# "~/NextPage.aspx?RowIndex=" +
Container.DataItemIndex %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Step 2.) In the Page Load event of NextPage.aspx, get first the GridViewRow where LinkButton was clicked and then read the row values
Note that using the PostBackUrl property in essence makes a cross page postback and therefore you can use the property Page.PreviousPage
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.PreviousPage != null)
{
int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
GridViewRow row = GridView1.Rows[rowIndex];
//Since you use Bound Fields, use row.Cells[] to read values
String Column_One_Value = row.Cells[0].Text;
String Column_Two_Value = row.Cells[1].Text;
}
}
Too long is subjective, anyways, if you don't want to use QueryString you can use Session Variables, Session vars are kept across the web site
protected void gvUsers_PageIndexChanging(object sender, EventArgs e)
{
Session["TheSelectedRow"] = ((Control)sender).Parent as GridViewRow;
// Your code to redirect
}
or you can wrap them in Properties, to work better and avoid casting every time in different lines and spelling mistakes
public GridViewRow TheSelectedRow
{
get { return Session["TheSelectedRow"] == null ? null : Session["TheSelectedRow"] as GridViewRow; }
set { Session["TheSelectedRow"] = value; }
}
and then in your click event this
protected void gvUsers_PageIndexChanging(object sender, EventArgs e)
{
TheSelectedRow = ((Control)sender).Parent as GridViewRow;
// Your code to redirect
}

How to add tooltips to button define behind with C#

I defined buttons in a for loop in behind with C#
protected void GenTable()
{
for (int r = 0; r < 100; r++)
{
var buttonField = new ButtonField
{
ButtonType = ButtonType.Button,
Text = "Model",
CommandName = "Display",
};
Model.Columns.Add(buttonField);
break;
}
}​
How can I add tooltips to these buttons?
I trid to add tooltip or title into the for loop but doesn't work.
Can anyone help me fix it? Thanks
There is no option to do what you want with a System.Web.UI.WebControls.ButtonField.
However, there is a possible workaround. You can set its Text property as it was an HTML, e.g
<asp:ButtonField Text="<span title='My beautiful button'>Model</span>" />
Update
Since you're setting the Text through C#, it seems that they unescape the <, > characters.
So, another workaround is by using the RowDataBound event. E.g: Given a GridView like this:
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" runat="server">
With the first column as follows:
<asp:ButtonField Text="Model" />
You can use the event ItemDataBound event below:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
(((System.Web.UI.WebControls.WebControl)e.Row.Cells[0].Controls[0])).ToolTip = "My beautiful button";
}
}
Note: change the Cells[0] to the index of the column you want, so if you have created 100 columns, and want to show 100 tooltips (like your code does), you must loop there and change to Cells[i].

Adding Links to grid cells(autogenerated gridview using C#), and getting information based on what user clicked

I autogenerate a column of values from a List UserID; which is generated using stored Oracle procedure, which takes input string(if...id Like "%string%" return refcursor) ; So the UserId list looks like that in the grid:
UserID
1111555
2222111
3331113
...
Now based on search parameters lenth, this column migh be from 1 record to 10(if UserID.count>10 then error message),so everytime we see different values in the grid
My questrion is how to put links on these autogenerated UserID's(which are in cells) that when user cliks on one of them I could get the selected id(1111555) and using it show another grid to the user:)
It's probably very simple...
You can handle the OnRowDataBound event as so:
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[0].Text = ""+e.Row.Cells[0].Text+"";
}
}
Where the UserID is the first column in the grid (Cell[0]). Here's the markup:
<asp:gridview id="GridView1"
autogeneratecolumns="true"
allowpaging="true"
onrowdatabound="GridView1_RowDataBound"
runat="server">
</asp:gridview>

How to disable a control in command field control in gridview

how to find a command field control in the gridview.
in a method not in the row data bound.
so far i have used this coding but i cant find the control.
<asp:CommandField ButtonType="Image" ShowEditButton="True
HeaderText="Enter Leave"
EditImageUrl="~/IMAGES/edit-icon.gif">
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
source code:
ImageButton edit = (ImageButton)EmployeeDetails.FindControl("Image");
edit.Enabled = false;
You can disable column itself with,
GridView1.AutoGenerateEditButton = false;
from code behind pages.
Or you can use ItemTemplate instead of CommandField,
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="id" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
And at code behind you can iterate through rows of GridView and disable each LinkButton.
foreach(GridViewRow gvr in GridView1.Rows)
{
LinkButton row = gvr.FindControl("id") as LinkButton;
row.Enabled = false;
}
First Edit :
I tried my second solution and it works. However, make sure your GridView is filled before you use foreach. Otherwise, GridView.Rows.Count would probably be 0.
Second Edit :
This works for CommandField too. Replace 0 with the location of CommandField in your GridView.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Enabled = false;
}
}
You miss to specify the row
Something like :
ImageButton edit = (ImageButton)EmployeeDetails.Rows[0].Cells[0].FindControl("Image");
edit.Enabled = false;
If you want to disable the column that contains the imageButton , you can do :
EmployeeDetails.Columns[0].Visible = false;
Try this:
try to hide controls at DataBound or RowDataBound event of GridView
protected void EmployeeDetails_DataBound(object sender, EventArgs e)
{
ImageButton edit = (ImageButton)EmployeeDetails.Row.Cells[0].FindControl("Image");
edit.Visible = false;
edit.Enabled = false; //OR use this line
}
particular column can be disabled in the following way
EmployeeDetails.Columns[0].Visible = false;
Hope this helps.
I had a similar issue. I simply disabled the view of the Column in BindData() function.
GridView1.Columns[0].Visible = false;
This worked for me, since my first column was Edit column and I have to enable it for specific users only.
Good luck!
Cast it as a DataControlFieldCell and then set Enabled to false.
Where: row.Controls[0] is your CommandField control
foreach (GridViewRow row in ManageDNXGridView.Rows)
{
DataControlFieldCell editable = (DataControlFieldCell)row.Controls[0];
editable.Enabled = false;
}

How can I access DataGridRow from a textbox on that row?

In a DataGrid, when text in a textbox changes I want to add the value of another field in that row to an array.
public void txtTitle_TextChanged(object sender, EventArgs e)
{
TextBox titleBox = (TextBox)sender;
DataGridItem myItem = (DataGridItem)titleBox.Parent.Parent;
string test = DataBinder.Eval(myItem.DataItem, "prod_id").ToString();
}
However myItem.DataItem evaluates as null. I was expecting it to evaluate as DataRowView?
You can get the TextChanged event to fire if you do the following:
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False"
onitemdatabound="DataGrid1_ItemDataBound">
<Columns>
<asp:TemplateColumn HeaderText="Test">
<ItemTemplate>
<asp:TextBox OnTextChanged="txtBox_TextChanged" ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Name" HeaderText="Test 1"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
You will notice that i have the following properties set:
AutoPostBack="True"
I have also manually added the OnTextChanged="txtBox_TextChanged" to the text box as well.
In my code behind i have:
protected void txtBox_TextChanged(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
Label1.Text = txtBox.Text;
}
The only way the event will fire is when you lose focus on the text box after typing.
Key points to consider:
This will cause a post back, so Ajax might be a good way to keep the user experience nice.
You will need to make sure you wrap your DataBind() in a if (!IsPostBack)
Hope this helps!
Effectively, I solved this by adding an autonumber column to the table, and using the value of this to determine the row's positino in the table, then using the value of this to affect the appropriate row in the datagrid.
I'm now merely changing the color of the row rather than adding values in that row to an array, as stated in the original question.
public void txtPrice_TextChanged(object sender, EventArgs e)
{
TextBox txtPrice = (TextBox)sender;
DataGridItem myItem = (DataGridItem)txtPrice.Parent.Parent;
markRows(myItem, true);
}
public void markRows(DataGridItem myItem, bool toSave)
{
// Prepeare to save this record?
CheckBox thisSave = (CheckBox)myItem.FindControl("chkSave");
thisSave.Checked = toSave;
// Establish the row's position in the table
Label sNo = (Label)myItem.FindControl("SNo");
int rowNum = Convert.ToInt32(sNo.Text) - 1;
CheckBox rowSave = (CheckBox)grid.Items[rowNum].FindControl("chkSave");
// Update background color on the row to remove/add highlight
if (rowSave.Checked == true)
grid.Items[rowNum].BackColor = System.Drawing.Color.GreenYellow;
else
{
Color bgBlue = Color.FromArgb(212, 231, 247);
grid.Items[rowNum].BackColor = bgBlue;
// some code here to refresh data from table?
}
}

Categories