Hallo there
I have an ImageButton control as part of a GridView control that is displayed as an ItemTemplate and in the same GridView I have a regular Button control to which I added some code like this.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "addToSession")
{
//get the row index stored in the CommandArgument property
int index = Convert.ToInt32(e.CommandArgument);
//get the gridview row where the command is raised
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
//values stored in the text property of the cells
string ISBN = selectedRow.Cells[0].Text;
string bookTitle = selectedRow.Cells[1].Text;
string image = selectedRow.Cells[2].Text;
Service s = new Service();
Session["ISBN"] = ISBN;
Session["bookTitle"] = bookTitle;
Session["ImageUrl"] = s.returnImageUrl(bookTitle);
if (Session["userName"] == null)
{
Response.Redirect("registerPage.aspx");
}
else
{
Response.Redirect("RateBook.aspx");
}
}
else if (e.CommandName == "ratedBooks")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
string bookTitle = selectedRow.Cells[1].Text;
Service s = new Service();
Session["ImageUrl"] = s.returnImageUrl(bookTitle);
Response.Redirect("BookRated.aspx");
}
when I run this code I get a format exception and again I am not sure why.
I have altered the image button a bit and nested the image in a link button which seems to be more correct.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="ratedBooks">
<asp:Image ID="ImageButton1" ImageUrl='<%#Eval("pictureUrl") %>'
runat="server" />
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Advice perhaps as to what to do
regards
ImageButton.CommandName is a valid property and it should work the same as Button.CommandName.
The problem, then, is most likely in your GridView1_RowCommand procedure. Can you post the entire procedure code including the part intended to handle the ImageButton click?
Related
I have a GridView that bound some data from DB and have a label in the ItemTemplate and EditTextbox in the EditItemTemplate they are in the same TemplateField. In the GridView some user has no data in somewhere field, If I want to insert a data for these user, I need to find GirdView label first and var the edittextbox, when updating I can Compare the value of them , such as the edittextbox value not equals to labeldata value then insert,
but I can't find the value of the label when rowupdating or databound of gridview
how can i do it ?
I have try
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int rowCount = GridView1.Rows.Count;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (rowCount >= 1)
{
Label lbDA_TEL_HK_NO = ((Label)e.Row.FindControl("lblKM_TEL"));
Session["DA_TEL_HK_NO"] = lbDA_TEL_HK_NO.Text;
}
}
}
It can find all the gridview data but not which I selected
P.S I'm a newbie, please help me
<asp:TemplateField ItemStyle-Width = "150px" HeaderText = "香港內線">
<ItemTemplate>
<asp:Label ID="lblHK_TEL" runat="server"
Text='<%# Eval("[DA_TEL_HK_NO]")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtHK_TEL" runat="server" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" MaxLength="3"
Text='<%# Eval("[DA_TEL_HK_NO]")%>'></asp:TextBox>
</EditItemTemplate>
You can try something like this for read all rows in grid view:
for (var i = 0; i < GridView1.Rows.Count; i++)
{
var label = GridView1.Rows[i].FindControl("lblKM_TEL") as Label;
if (label != null)
{
// Manipulate label control
}
}
Or you can gets label from selected row:
var label = GridView1.SelectedRow.FindControl("lblKM_TEL") as Label;
if (label != null)
{
// Manipulate label control
}
From your aspx markup you are using the wrong label id .
you need yo use this label id lblHK_TEL
You code looks as follows after changes
Label lbDA_TEL_HK_NO = ((Label)e.Row.FindControl("lblHK_TEL"));
I have a GridView in a ASP.NET web application, in which I have added image button in each row:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="edit" runat="server" CommandArgument='<%# Bind("EmpID") %>' CommandName="edituser" ImageUrl="image/images.jpg"
ToolTip="Edit User Details"> </asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
Now how I can get the row data from gridview simply by clicking an edit image button in a row?
You have to change the CommandArgument with this one:
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
Then:
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "edituser") /*if you need this
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the buttonfrom the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here now you have the specific row data
}
}
Bind all the columns in the label control respectively, and you can get value using findcontrol method at GridView1_SelectedIndexChanged event
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Label _lblText = (Label)this.GridView1.SelectedRow.FindControl("UR_Desired_Label_ID");
this.TextBox3.Text = _lblText.Text ;
}
I have an hiddenfield field in my gridview but the code behind cant get its value maybe someone could find the problem.
HTML:
<asp:TemplateField HeaderText="TweetID" Visible="false">
<ItemTemplate>
<asp:HiddenField ID="TweetID" runat="server" Value='<%#Eval("TweetID") %>' />
</ItemTemplate>
</asp:TemplateField>
.cs:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
HiddenField tid = GridView1.Rows[index].FindControl("TweetID") as HiddenField;
//Response.Write(tid.Value);
TweetHelper.RemoveTweet( Convert.ToInt32(tid.Value), 1);
}
by the way the response writes nothing.
Based on your code above what you are doing is overkill.
Either make TweetID a Gridview.DataKey.
Or if that's not an option, convert your Delete button to a template field and add TweetID as a CommandArgument to the Delete button.
Your code should work fine.However another way to find the control is
GridViewRow row = GridView1.Rows[e.RowIndex];
HiddenField hdn = (HiddenField)row.FindControl("TweetID");
string value = hdn.Value;
or simply
var tweetid = ((HiddenField)GridView1.Rows[e.RowIndex].FindControl("TweetID")).Value;
In my web application, I have a grid-view and this grid-view is databounded. Above this grid-view, I have a text box where user can first enter the Employee ID and show the Employee Name in the grid view. In the grid view, it has the edit icon. When the user hit that edit icon, some text boxes in the grid view will appear and the use can edit the text in the text box. I got it working so far. However, my problem is when I am trying to edit the empty text box in the grid view, it gave me this error:
Object is not set to an instance of an object
All I did is to check if the text box is null. This does ignore the error but not really what I want
protected void txtEditedEmployeeID_TextChanged(object sender, EventArgs e)
{
DatabaseManager dbManager = Common.GetDbManager();
foreach (GridViewRow row in gridViewAddEmployee.Rows)
{
TextBox txtEmployee= (TextBox)row.FindControl("txtEmployeeID");
if (txtEmployee!= null)
{
string personID = txtEmployee.Text;
DataSet dsRE = dbManager.GetEmployeeNameByID(personID);
for (int i = 0; i < dsRE.Tables[0].Rows.Count; i++)
{
string employeeFirstName = dsRE.Tables[0].Rows[i]["FIRST_NAME"].ToString();
string employeeLastName = dsRE.Tables[0].Rows[i]["LAST_NAME"].ToString();
((TextBox)row.FindControl("txtEmployeeName")).Text = staffLastName + " " + staffFirstName;
}
}
else
{
}
}
}
HTML ASP.NET Code
<asp:TemplateField HeaderText="EmployeeID">
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeID" runat="server" AutoPostBack="true" OnTextChanged= "txtEditedEmployeeID_TextChanged" Text='<%#Bind("Employee_ID") %>'Width="90px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblEmployeeID" runat="server" CssClass="GridInput" Text='<%#Bind("Employee_ID") %>'Width="90px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Name">
<EditItemTemplate>
<asp:TextBox ID="txtEmployee" runat="server" ReadOnly ="true" Text='<%#Bind("Full_Name") %>'Width="90px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblEmployeeName" runat="server" CssClass="GridInput" Text='<%#Bind("Full_Name") %>'Width="90px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
As on Edit txtEmployeeID will appear for that row only Not for all the rows of GridView and on TextBox Change event you are finding all row edited TextBox which is not appear till now so you may try this:
protected void txtEditedEmployeeID_TextChanged(object sender, EventArgs e)
{
DatabaseManager dbManager = Common.GetDbManager();
TextBox txt = sender as TextBox; // Edited TextBox
GridViewRow row = (txt.NamingContainer as GridViewRow);// Row of Edited TextBox
TextBox txtEmployee= (TextBox)row.FindControl("txtEmployeeID");
string personID = txtEmployee.Text;
DataSet dsRE = dbManager.GetEmployeeNameByID(personID);
for (int i = 0; i < dsRE.Tables[0].Rows.Count; i++)
{
string employeeFirstName = dsRE.Tables[0].Rows[i]["FIRST_NAME"].ToString();
string employeeLastName = dsRE.Tables[0].Rows[i]["LAST_NAME"].ToString();
((TextBox)row.FindControl("txtEmployeeName")).Text = staffLastName + " " + staffFirstName;
}
}
Try this:
Instead of
TextBox txtEmployee= (TextBox)row.FindControl("txtEmployeeID");
if (txtEmployee!= null)
{
string personID = txtEmployee.Text;
You should write it like this
TextBox txtEmployee = new TextBox();
txtEmployee = (TextBox)row.FindControl("txtEmployeeID");
if (!string.isNullorEmpty(txtEmployee.Text))
{
string personID = txtEmployee.Text;
I am using a CommandField Delete imagebutton but it does not work. here is a snippet which i have tried:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
int indx = row.RowIndex;
using (Property_dbDataContext context = new Property_dbDataContext())
{
if (e.CommandName == "Delete")
{
var delete = (from a in context.Property_basics where a.prop_id == GridView1.Rows[indx].Cells[0].Text select a).Single();
delete.delete_status = 1;
context.SubmitChanges();
GridView1.DeleteRow(indx);
}
}
}
.aspx:
<asp:CommandField ButtonType="Image" HeaderText="Delete" DeleteImageUrl="~/wp-content/themes/realia/assets/img/500px-Delete_Icon2.png" ShowDeleteButton="True" />
the exception which it shows is:
Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type
'System.Web.UI.WebControls.GridViewRow'.
Instead of searching the row you should use the CommandArgument to get the RowIndex. In the case of a CommandField the CommandArgument is the RowIndex. In the case of the CommandArgument passed on a standard control it's the actual command argument.
int rowIndex = int.Parse(e.CommandArgument);
GridViewRow row = GridView1.Rows[rowIndex];
try this:
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer.Parent.Parent;
Or Like
Setting Command Arguments
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = ProductsGridView.Rows[index];
Your ASPX will look like this :
<asp:Gridview id = Gridview1 runat = "server" DataKeyNames="ID" >
Code Behind : You can do it like this,
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(dg1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)dg1.Rows[e.RowIndex];
// write your code. to delete
}