I have 3 GridView controls in 3 different pages.They worked fine before.
Suddenly I got this error message:
Invalid postback or callback argument. Event validation is enabled using <pages enableeventvalidation="true" />
So I tried to put <#pages enableeventvalidation="false" /> in the page. It still did not work.
I deleted this command from page. Then another message shows up " Index was out of range" when click the buttons in the gridview for all the girdview controls.
int id = Convert.ToInt32(myGridView.DataKeys[e.RowIndex].Value)
It looks like the gridview could not find the datakey (e.rowIndex's value is OK), Datakeynames has been set.
protected void gvItemCategory_RowDeleting(object sender, GridViewDeleteEventArgs e) {
int categoryId = Convert.ToInt32(gvItemCategory.DataKeys[e.RowIndex].Value);
CollectionCategory category = new CollectionCategory();
category.CategoryId = categoryId;
category.Delete();
ItemCateogryShowData();
}
HTML:
<asp:GridView ID="gvItemCategory" runat="server" AutoGenerateColumns="False"
OnRowCancelingEdit="gvItemCategory_RowCancelingEdit"
OnRowDeleting="gvItemCategory_RowDeleting" OnRowEditing="gvItemCategory_RowEditing"
DataKeyNames="CategoryId" OnRowUpdating="gvItemCategory_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="CateogoryName">
<ItemTemplate>
<asp:Label ID="lblItemCategoryName" runat="server" Text='<%#Eval("CategoryName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbItemCateogryName" runat="server" Text='<%#Eval("CategoryName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ButtonType="Button" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Try binding your Gridview on pageload !ispostback method..
If(!isPostback)
{
//your code here
}
Try this
int categoryId = Convert.ToInt32(gvItemCategory.DataKeys[e.RowIndex].Value.Tostring());
NOTE:Also use EnableeventValidation="false"
Related
This is my html mark-up and adding the visibility to false in the div tag hides the actual data itself, but just leaves a blank column. I tried to access the div tag (yes I added the runat="server" tag to the html) and attempted to access it like so hideme.Visible = true; which threw a compile error of
Does not exist in the current context.
What should I alter/modify to ensure that this column is completely hidden from my grid?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="true"
onrowdatabound="GridView1_RowDataBound" onrowcreated="GridView1_RowCreated">
<Columns>
<asp:BoundField DataField="abcd" HeaderText="abcd" />
<asp:BoundField DataField="def" HeaderText="def" />
<asp:BoundField DataField="hij" HeaderText="hij" />
<asp:BoundField DataField="xyz" HeaderText="xyz" />
<asp:BoundField DataField="eee" HeaderText="eee" />
<asp:BoundField DataField="era" HeaderText="era" />
<asp:BoundField DataField="nai" HeaderText="nai" />
<asp:BoundField DataField="fac" HeaderText="fac" />
<asp:TemplateField>
<ItemTemplate>
<div runat="server" style="visibility:hidden" id="hideme">
<asp:Label ID="lbllunch" runat="server" Text='<%# Eval("hij") %>' />
<asp:Label ID="lbllunchoverage" runat="server" Text='<%# Eval("xyz") %>' />
<asp:Label ID="lbleee" runat="server" Text='<%# Eval("eee") %>' />
<asp:Label ID="lblera" runat="server" Text='<%# Eval("era") %>' />
<asp:Label ID="lblnai" runat="server" Text='<%# Eval("nai") %>' />
<asp:Label ID="lblfac" runat="server" Text='<%# Eval("fac") %>' />
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
EDIT
I added the .Visible command to my page load event (where I always hide anything on my page) like so:
protected void Page_Load(object sender, EventArgs e)
{
hideme.Visible = false;
/More here
}
Since hideme is inside the GridView TemplateField, you can't access it in Page_Load method, however you can access it in GridView1_RowDataBound method as below
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// find the hideme div
HtmlGenericControl div = (HtmlGenericControl)e.Row.FindControl("hideme");
// set the visible property
div.Visible = false;
}
}
You can hide the column in DataBound event of gridview.
protected void GridView_DataBound(object sender, GridViewRowEventArgs e)
{
GridView.Columns[8].Visible = false;
}
If you want to hide template field column why you are adding it to markup. Yes if you keep style="visibility:hidden" it will show blank column only because item template still exists as column.
My suggestion for you is if dont need column dnt add markup.
I'm added a template field and made it a button click event. Then I'm trying to sent the value to event method and want to some operation with that value. I wrote following code. It has no compilation error but when i click Present browser shows following message:
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Code:
<div>
<asp:Panel ID="PanelAllEmployee" runat="server" Height="400px">
<asp:GridView ID="GridViewAllEmployee" runat="server" AutoGenerateColumns="False" style="position:absolute;left:219px; top:202px;" >
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="EmailID" HeaderText="Email ID" />
<asp:BoundField DataField="Position" HeaderText="Position" />
<asp:TemplateField HeaderText="Present">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandArgument='<%#Eval("Id") %>' OnClick="Button1_Click" Text="Present" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"> </asp:Label>
</asp:Panel>
</div>
Event Method:
protected void Button1_Click(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
string id = lnk.CommandArgument.ToString();
Label1.Text = id;
}
Please be kind to help me. I'm beginner. Elaborate answer is much appreciated. Thanks for your time.
set
EnableEventValidation="false" in the page directive in code-behind
for ex:
<%# Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
EDIT: .aspx :
<asp:Button ID="Button1" runat="server"
OnClick = "Button1_Click" CommandArgument='<%#Eval("Id") %>' Text="Present" />
cs code:
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string id = btn.CommandArgument.ToString();
}
using ButtonField and an OnRowCommand Event
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
OnRowCommand = "OnRowCommand">
<Columns>
<asp:ButtonField CommandName = "ButtonField" DataTextField = "CustomerID"
ButtonType = "Button"/>
</Columns>
</asp:GridView>
Code Behind:
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridView1.Rows[index];
}
I'm doing some complicated things with a gridview and the only thing standing between me and completion is the fact that I can't set the databinding programmatically. I've made it most of the way around this issue except that I still cannot get the datasource to update.
I've replaced one input field with a dependent dropdownlist and gotten it to retain the value of the field on databind. I can't databind it directly because I get 'Selected Value not in list of items' or something, so I have to find a way to get the grid or the datasource to take the value from this dependent drop down and apply it to the table.
Any help?
<asp:GridView ID="gvManager" runat="server"
AutoGenerateColumns="False" DataSourceID="ldsCampaigns"
ondatabound="gvManager_DataBound"
onrowediting="gvManager_RowEditing" DataKeyNames="ContentID">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="ContentID" HeaderText="ContentID"
SortExpression="ContentID" />
<asp:BoundField DataField="CampaignName" HeaderText="CampaignName"
SortExpression="CampaignName" />
<asp:BoundField DataField="CampaignTitle" HeaderText="CampaignTitle"
SortExpression="CampaignTitle" />
<asp:BoundField DataField="CampaignTagLine" HeaderText="CampaignTagLine"
SortExpression="CampaignTagLine" />
<asp:TemplateField HeaderText="CampaignData" SortExpression="CampaignData">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ldsTables"
DataTextField="CMSTables" DataValueField="CMSTables"
SelectedValue='<%# Bind("CampaignData") %>' onload="DropDownList1_Load">
</asp:DropDownList>
<asp:LinqDataSource ID="ldsTables" runat="server"
ContextTypeName="DataContext"
onselecting="ldsTables_Selecting" Select="new (CMSTables)"
TableName="CampaignTables">
</asp:LinqDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CampaignData") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DataColumn"
SortExpression="DataColumn">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" style="margin-bottom: 0px"
SelectedValue='<%# Bind("DataColumn") %>' Visible="False">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# Bind("DataColumn") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CampaignLink" HeaderText="CampaignLink"
SortExpression="CampaignLink" />
<asp:BoundField DataField="Sunrise" HeaderText="Sunrise"
SortExpression="Sunrise" />
<asp:BoundField DataField="Sunset" HeaderText="Sunset"
SortExpression="Sunset" />
<asp:BoundField DataField="OwnerID" HeaderText="OwnerID"
SortExpression="OwnerID" />
<asp:CheckBoxField DataField="Enabled" HeaderText="Enabled"
SortExpression="Enabled" />
</Columns>
</asp:GridView>
If i understood you question right, you have a gridview and inside the gridview there is a dropdownlist, on changing the dropdownlist you want to get it's value and fetch some data or whatever you want with it, if that is the case then what you haved is the following:
1-Set your dropdownlist property AutoPostBack = "true".
2-On the dropdownlist_indexchanged event for the dropdownlist do the following:
protected void dropdownlist1_indexchanged(sender object,eventargs e)
{
//Because the dropdownlist is inside the gridview you can't get data directly
//You will have to parse the sender object which will retrieve the dropdownlist
DropdownList ddl = (DropdownList)sender;
//You can then extract the data from the dropdownlist
}
For any future desperate programmer, I had to update the linq data source code on the updated event:
protected void ldsCampaigns_Updated(object sender, LinqDataSourceStatusEventArgs e)
{
using (DashboardDataContext c = new DashboardDataContext())
{
Label Label3 = gvManager.Rows[gvManager.EditIndex].FindControl("Label4") as Label;
List<CMSCampaigns> change = c.CMSCampaigns.Where(s => s.ContentID == Convert.ToInt16(Label3.Text)).ToList();
DropDownList DropDownList2 = gvDashboardManager.Rows[gvDashboardManager.EditIndex].FindControl("DropDownList2") as DropDownList;
change[0].DataColumn = DropDownList2.SelectedValue.ToString();
c.SubmitChanges();
}
}
This is the clusteriest clusterkludge I've ever kludgestergrammed.
How can I select all data from GridViews current row..
I have a column for edit link in GridView. When "Edit" link button is clicked, I want to use that selected row's data. I am trying the following code, but it's returning me with an empty value
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = -1;
GridViewRow gvRow = gv.Rows[ e.NewEditIndex];
string selectedID = gvRow.Cells[3].Text;
}
<asp:GridView runat = "server" ID="gvRange0" SkinID="gridView" AutoGenerateColumns="False"
AllowSorting="True" OnRowCancelingEdit="gvRange_RowCancelingEdit" OnRowDeleting="gvRange_RowDeleting"
OnRowEditing="gvRange_RowEditing" OnRowUpdating="gvRange_RowUpdating"
Width="684px" OnRowDataBound="gvRange_RowDataBound"
DataMember="DefaultView" OnPageIndexChanged="gvRange_PageIndexChanged"
OnPageIndexChanging="gvRange_PageIndexChanging" OnSorting="gvRange_Sorting" DataKeyNames = "RANGE_ID"
OnSelectedIndexChanged="gvRange_SelectedIndexChanged" Height="65px" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<ControlStyle Width="2px" />
<asp:LinkButton ID="lnkDelete0" runat="server" CssClass="lnk"
CausesValidation="False" CommandName="Delete"
Text="Delete" Visible="false"></asp:LinkButton>
<asp:CheckBox runat="server" ID="chkSelect" CssClass="lbl" Text="" AutoPostBack="False" OnCheckedChanged="chkSelect_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<controlStyle width="2px" />
<asp:LinkButton ID="lnkEdit" runat="server" CssClass="lnk" CausesValidation="False" CommandName="Edit"
Text="Edit" ></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="5px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Ranges" SortExpression="Sort_Ranges">
<ControlStyle Width="5px" />
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"Min_Age") %>
<%# CheckNull(DataBinder.Eval(Container.DataItem,"Max_Age")) %>
</ItemTemplate>
<%-- <ItemTemplate>--%>
<%--<asp:Label ID="lblStageName" CssClass="lbl" runat="server" Text='<%# Bind("Age_Range") %>' Width="1px"></asp:Label>--%>
<%-- </ItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Range ID">
<ItemTemplate><%#DataBinder.Eval(Container.DataItem,"RANGE_ID") %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
There are 4 columns in the GridView. One contains check box, second is link button for edit, third databound with some value, fourth is the column which I want to use to get some values from database(that is primary key there) and this column is hidden.
sometime in gridview cell create child controls.
you can try this code. may be solve it.
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = -1;
GridViewRow gvRow= (GridViewRow)(((Button)e.CommandSource).NamingContainer);
foreach (TableCell Tc in gvRow.Cells)
{
//if you are not getting value than find childcontrol of TabelCell.
string sss = c.Text;
foreach (Control ctl in Tc.Controls)
{
//Child controls
Label lb = ctl as Label;
string s = lb.Text;
sb.Append(s + ',');
}
}
}
I've noticed that you say you need to access the 4th column but you're using gvRow.Cells[3].Text;
The indexing in the Cell object is done from 1, so if you need to access the 4th row in the grid view try this:
string selectedID = gvRow.Cells[4].Text;
EDIT:
Could you please confirm two things for me
1)When you click on lnkEdit is the GridView1_RowEditing event being raised?
2)If yes, is the e.NewEditIndex value always showing up as '0'?
Attempt clicking the edit link on different rows, is the result always '0'?
I have a gridview which displays the contents of a database table in rows. There is a CheckboxField there and a Select button. I want to set button visibility to false when checkboxfield is checked.
this is my aspx page:
<asp:DetailsView ID="DetailsViewERgo" runat="server" Height="50px"
Width="100%" AutoGenerateRows="False" CellPadding="4"
DataSourceID="LinqDataSourceErgo" ForeColor="#333333" GridLines="None"
HeaderText="Σύντομη Περιγραφή Επιλεγμένου Έργου">
<Columns>
<asp:CheckBoxField DataField="Diekperewsi" HeaderText="Answered"
SortExpression="Diekperewsi" Visible="True"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" />
</asp:CheckBoxField>
<asp:TemplateField HeaderText="Insert Answer" ShowHeader="False">
<ItemTemplate>
<center>
<asp:Button ID="Button1" runat="server" CausesValidation="False"
CommandName="Select" Text="Επιλογή" Visible="true" >
</asp:Button>
</center>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have tried this but only works with checkboxes
protected void GridViewAitima_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)e.Row.FindControl("Diekperewsi");
Button b = (Button)e.Row.FindControl("Button1");
if (!cb.Checked)
{
b.Visible = false;
}
else
{
b.Visible = true;
}
}
}
Your code will run on the server side but it looks as if the the AutoPostBack property for your checkbox is not set to true -
AutoPostBack="True"
so when the checkbox is checked the code will not run immediately, it will only run after another event has caused your page to postback.
CheckBoxField has no id thus you can't find it by id, moreover it has no value propertie.
I suggest you use template field just like you used for the button, but instead put a checkbox in it.
so instead of :
<asp:CheckBoxField DataField="Diekperewsi" HeaderText="Answered"
SortExpression="Diekperewsi" Visible="True"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" />
</asp:CheckBoxField>
put :
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="Diekperewsi" Enabled="false" Checked='<%#Eval("Diekperewsi")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
and you are good