Nesting Gridview inside Formview breaks insert codebehind context - c#

I have a couple of GridViews in my FormView page and I am wanting to make an Insert row in the FooterRow of the Gridviews. Layout and everything is fine. However, as I'm building the codebehind for the Insert command, I'm running into a context problem. If I move the GridView outside of the FormView markup, the context errors clear up immediately.
GridView Markup
<asp:GridView ID="gvBFMats" runat="server" ShowFooter="True" AutoGenerateColumns="False" DataKeyNames="MaterialID" DataSourceID="BFMatsSQL" OnRowCommand="gvBFMats_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Commands" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="ButtonUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="ButtonEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="ButtonDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="ButtonAdd" runat="server" CommandName="Insert" Text="Add to Table" />
</FooterTemplate>
...
Insert Command Codebehind
protected void gvBFMats_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert" && Page.IsValid)
{
BFMatsSQL.Insert();
}
}
protected void BFMatsSQL_Inserting
(object sender, ObjectDataSourceMethodEventArgs e)
{
DropDownList ddlNewMfr =
(DropDownList)gvBFMats.FooterRow.FindControl("ddlNewMfr");
DropDownList ddlNewThickness =
(DropDownList)gvBFMats.FooterRow.FindControl("ddlNewThickness");
DropDownList ddlNewCore =
(DropDownList)gvBFMats.FooterRow.FindControl("ddlNewCore");
DropDownList ddlNewSize =
(DropDownList)gvBFMats.FooterRow.FindControl("ddlNewSize");
TextBox txtNewColor =
(TextBox)gvBFMats.FooterRow.FindControl("txtNewColor");
TextBox txtNewQty =
(TextBox)gvBFMats.FooterRow.FindControl("txtNewQty");
DropDownList ddlNewFinish =
(DropDownList)gvBFMats.FooterRow.FindControl("ddlNewFinish");
TextBox txtNewExtra =
(TextBox)gvBFMats.FooterRow.FindControl("txtNewExtra");
// Set the SQLDataSource's InsertParameters values
e.InputParameters["MatManufacturerID"] =
Convert.ToInt32(ddlNewMfr.SelectedValue);
e.InputParameters["MatThicknessID"] =
Convert.ToInt32(ddlNewThickness.SelectedValue);
e.InputParameters["MatCoreID"] =
Convert.ToInt32(ddlNewCore.SelectedValue);
e.InputParameters["MatSizeID"] =
Convert.ToInt32(ddlNewSize.SelectedValue);
e.InputParameters["MatFinishPrePostID"] =
Convert.ToInt32(ddlNewFinish.SelectedValue);
string strNewColor = null;
if (!string.IsNullOrEmpty(txtNewColor.Text))
strNewColor = txtNewColor.Text;
e.InputParameters["MatFinishColor"] = strNewColor;
int? intNewQty = null;
if (!string.IsNullOrEmpty(txtNewQty.Text))
intNewQty = Convert.ToInt32(txtNewQty.Text);
e.InputParameters["MatQty"] = intNewQty;
string strNewExtra = null;
if (!string.IsNullOrEmpty(txtNewExtra.Text))
strNewExtra = txtNewExtra.Text;
e.InputParameters["MatNumExtraSheets"] = strNewExtra;
}
Specifically, I get the red squiggly under the gvBFMats in the (Control)gvBFMats.FooterRow.FindControl("Control ID"); that says "The name 'gvBFMats' does not exist in the current context." I'm only guessing that it doesn't like the call to the GridView when it's nested inside a FormView template. Is there a way to pass this context along programatically?

You're right about it not recognizing the name gvBFMats because it's embedded in a template. Only "top-level", non-embedded controls will be treated in Code Behind as if they had been explicitly declared. Controls declared in a template will not. The compiler doesn't recognize those names.
There's a reason for this. Imagine you have a control called TextBox1 in one of the ItemTemplates for a repeating control. You bind it. Your page's control tree now has dozens of controls with the ID TextBox1. If you want to refer to TextBox1, how does it know which one you mean?
So. What can you do in your situation? Well, BFMatsSQL_Inserting and gvBFMats_RowCommand are both event handlers, so you can't change their signatures.
But, you can make use of them belonging to the same class, and use a module-level variable to hold the reference to gvBFMats. Like this:
private GridView gvBFMats;
protected void gvBFMats_RowCommand(object sender, GridViewCommandEventArgs e)
{
gvBFMats = [your form view].Row.FindControl("gvBFMats") as GridView;
if (e.CommandName == "Insert" && Page.IsValid)
{
BFMatsSQL.Insert();
}
}
Now, BFMatsSQL_Inserting will be able to refer to gvBFMats, and it should have a value.

Related

how to get value of a template field label of a gridview, getting error Object reference not set to an instance of an object

While getting the value I'm getting Object reference not set to an instance of an object. how to get labeled label value in code behind. How to get the value in custom event
<asp:GridView runat="server" ID="gridviewQuoteDetails" EmptyDataText="No records Found..." AutoGenerateEditButton="false" OnRowEditing="gridviewQuoteDetails_RowEditing" OnRowUpdating="gridviewQuoteDetails_RowUpdating" DataKeyNames="id" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href='Quote.aspx?val=<%#Eval("id")%>'>
<asp:Label ID="lblid" runat="server" Text='<%#Eval ("id")%>'></asp:Label>
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" Text="Update" runat="server" OnClick="OnUpdate" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind
protected void OnUpdate(object sender, EventArgs e)
{
string qouteid = ((Label)gridviewQuoteDetails.FindControl("lblid")).Text;
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string id = (row.Cells[0].Controls[0] as TextBox).Text;
string Description = (row.Cells[1].Controls[0] as TextBox).Text;
}
Gridview is a collection of rows right, but you are trying to search the control directly within the gridview thus ((LinkButton)gridviewQuoteDetails.FindControl("lbllnknm")) will be null and when you are trying to access the Text property of LinkButton you are getting NRE error.
You need to loop through the rows of your gridview control to access each linkbutton present in each row like this:-
foreach (GridViewRow row in gridviewQuoteDetails.Rows)
{
LinkButton lbllnknm= row.FindControl("lbllnknm") as LinkButton;
}
But ideally you would not be needing this. I guess you are trying to get the value in OnRowEditing event handler. If that is the case then you can fetch the value of linkbutton like this:-
protected void gridviewQuoteDetails_RowEditing(object sender, GridViewUpdateEventArgs e)
{
string qouteid = ((Label)gridviewQuoteDetails.Rows[e.NewEditIndex]
.FindControl("lbllnknm")).Text;
}
Update 2:
As per your comment you are trying to access the LinkButton text on click of LinkButton itself which means the event is raised by LinkButton itself. You can simply use the sender object like this:-
protected void OnUpdate(object sender, EventArgs e)
{
LinkButton LinkButton2 = sender as LinkButton;
GridViewRow row = LinkButton2.NamingContainer as GridViewRow; //Get the gridview row
Label lblid = row.FindControl("lblid") as Label;
string qouteid = lblid.Text;
}

Getting Gidview Hidden field value on button click in ASP.NET

I have a Gridview dtAppend. I want that when I press delete button the selected row record should be deleted from users table.
I first used button field in gridview, as:
<asp:ButtonField Text="Delete" CommandName="DeleteRow" ControlStyle-CssClass="btn btn-danger btn-small" ControlStyle-ForeColor="White" />
<asp:TemplateField visible="false" ItemStyle-Width="0px">
<ItemTemplate>
<asp:HiddenField ID="HiddenField" Visible="false" runat="server" Value='<%# Eval("userId") %>' />
</ItemTemplate>
</asp:TemplateField>
My client says to show JavaScript alert and on clicking yes the record should be deleted. I cannot write onClientClick for button field so I am being forced to use normal Asp button.
on rowCommand of gridview I am getting the hidden field value in this code
if (e.CommandName == "DeleteRow")
{
GridViewRow row = dtAppend.Rows[Convert.ToInt32(e.CommandArgument)];
hidden1 = (HiddenField)row.Cells[6].FindControl("HiddenField");
string text = Convert.ToString((HiddenField)row.Cells[6].FindControl("HiddenField"));
Session["dtIdDel"] = hidden1.Value;
}
i am getting thew value in Session but i need above code working Button_ClickEvent like below
protected void deleteButton_Click(object sender, EventArgs e)
{
GridViewRow row = dtAppend.Rows[Convert.ToInt32(e.CommandArgument)];
hidden1 = (HiddenField)row.Cells[6].FindControl("HiddenField");
string text = Convert.ToString((HiddenField)row.Cells[6].FindControl("HiddenField"));
Session["dtIdDel"] = hidden1.Value;}
this is where 'e.CommandArgument' gives Error
I cannot use the above code in normal button click as it gives error in e.CommandArgument
Any help?
Simply you can remove visible="false"
<asp:HiddenField ID="HiddenField" runat="server" Value='<%# Eval("userId") %>' />
You Better Remove Visible="false" . Because, the value that has to be binded for hidden field will not be binded in to the field if Visible="false" is there. Any how its a hidden field, so make it Visible="true"
EDIT :
How you handled the RowDataBound event of the Grid, are you assigning the CommandArgument for each row, other wise the above concept will not work in Paging. Refer as below
Ex : -
Button btnMail = (Button)e.Row.FindControl("lnkMail");
btnMail.CommandArgument = e.Row.RowIndex.ToString();
I think this would be easy way, instead of using hidden field.
<asp:LinkButton CommandArgument='<%# Eval("userId") %>' OnClientClick="if (!confirm('Are you sure you want delete?')) return false;" CommandName="DeleteRow" ID="eliminar" runat="server" Text="delete"/>
if (e.CommandName == "DeleteRow")
{
int userId = Int32.Parse(e.CommandArgument.ToString());
}
You can simply send ID as command argument
or
Try the code as below:
var ID = int.Parse(((HiddenField)item.FindControl("HiddenField1")).Value);
sql = "delete from tablename where id=" + ID;

How to get datatextfield value

I am making a web site in visual studio 2012, using asp.net, c#.
I created a grid view, which is taking data from my sql server, and created a button field which is bound to id_p, which is one of the collumns taken from a database. I am trying to get the data of the id_p for the row in which the button was clicked.
<asp:ButtonField ButtonType="Button" DataTextField="id_p"
DataTextFormatString="Stavi u košaricu" Text="Button1" />
What I need is not the selected row, only the id_p value, so how could I do that, please?
You need to handle the OnRowCommand Event on the gridview as so:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView_RowCommand"
And create a ItemTemplateField to display a regular <asp:button> instead of using the ButtonField column:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn" runat="server"
CommandName="Something" CommandArgument='<%#Eval("id_p") %>'
Text="Stavi u košaricu" />
</ItemTemplate>
</asp:TemplateField>
Now you handle the RowCommand Event:
protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
string m_id = e.CommandArgument.ToString();
if (e.CommandName == "Something")
{
//do something with m_id
}
}

Unable to delete multiple grid view records

I am trying to delete multiple grid view records. I tried as shown below.
public void btnDelete_Click(object sender, EventArgs e)
{
StringCollection orderNumberCollection = new StringCollection();
if (gridOrders.Rows.Count > 0)
{
foreach (GridViewRow gvrow in gridOrders.Rows)
{
if (gvrow.RowType == DataControlRowType.DataRow) {
CheckBox cbx = (CheckBox)gvrow.Cells[0].FindControl("chkdelete");
Label lblOrderNumner = (Label)gvrow.FindControl("labelOrderNumber");
Label lastName = (Label)gvrow.FindControl("LabelLastName");
if (cbx.Checked && lblOrderNumner != null)
{
orderNumberCollection.Add(lblOrderNumner.Text);
}
}
}
}
if (orderNumberCollection.Count > 0)
{
DeleteMultipleOrders(orderNumberCollection);
}
}
But always check box control showing "Checked = false". Why check box control always showing false even I checked some of the check boxes?
Here is my Grid view code:
<asp:TemplateField>
<HeaderTemplate>
<table><tr><td ><asp:CheckBox ID="chkAll" runat="server" /></td><td><asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" /></td></tr></table>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkdelete" runat="server" Text='<%# Bind("OrderNumber") %>' Font-Bold="false" />
</ItemTemplate>
</asp:TemplateField>
I'm with Tim on this, it sounds like you are loading your gridview from within Page_Load, which is fine. but in your case you would need to make sure your gridview loading code looks like this:
if(!Page.IsPostBack)
{
//gridview loading code
}
this prevents your gridview from being reloaded (and losing which boxes are checked) when you click your delete button

Get the Current Selected Row of GridView on a button Click

I want to fetch the DataKey of the Selected Row of GridView on button click.
I would Personal do it in a Template Field Like so:
<asp:TemplateField>
<ItemTemplate>
//EDIT: after a comment it is suggested that you pass the RowIndex as the command argument which would provide access to the entire row
<asp:LinkButton ID="btnCopy" runat="server"CausesValidation="False"CommandName="MyCommandButton"CommandArgument='<%# Eval("MyDataKeyOrWhateverIWanteverIWantFromTheBindingSource")%>'>
</ItemTemplate>
</asp:TemplateField>
CodeBehind
protect void MyCommandButton(Object sender,CommandArgument e)
{
int DataKeyOrPK=int32.Parse(e.CommandArgument.ToString());
}
You other option would be:
<asp:gridview id="myGrid" runat="server"
width=100% datakeynames="Myid"
autogeneratecolumns=false
onSelectedIndexChanged="MyEvent">
<asp:templatefield headertext="Choose your dream home">
<itemtemplate>
<asp:linkbutton runat="server" commandname="select" text='<%# Eval ( "Whatever" ) %>' />
</itemtemplate>
</asp:templatefield>
Note the commandname="select" above.
Data-bound controls recognize certain command names and automatically raise and handle the appropriate events for the control. The following command names are recognized:
Cancel, Delete, Edit, Insert, New, Page, Select, Sort and Update. Reference
Codebehind
private void MyEvent(Object sender, EventArgs e)
{
string id = myGrid.SelectedDataKey.Value.ToString();
}
Use the SelectedDataKey property of the GridView:
DataKey currentKey = myGridView.SelectedDataKey;

Categories