Forms Data Controls :: Manually Adding GridViewRow Throwing Off Viewstate
Mar 1, 2010Manually Adding GridViewRow Throwing Off Viewstate
View 6 RepliesManually Adding GridViewRow Throwing Off Viewstate
View 6 RepliesI have a gridview i want to know how to get data of gridview row on button click
protected void Button1_Click(object sender, EventArgs e)
{
GridView grd = (GridView)((Button)sender).NamingContainer;
string name = grd.SelectedRow.Cells[0].Text;
}
[Code] .....
The below is from the watch window in vs2010.
((sender as EntityTextBox).NamingContainer as GridViewRow).UniqueID "ctl00$cphMainFrame$PurchaseControl1$PurchaseFormView$PurchaseRowGridView$ctl04"
I need to loop through all the gridviewrows, and call a function for every row using the datakey. How do I get the datakey of each row?
My code looks something like:
foreach(GridViewRow gvr in GridView1.Rows)
{
DoSomething(gvr.INSERT DATA KEY OF THIS ROW HERE);
}
I have the DataKey value, I want to use that to get the GridViewRow and retrieve information from various columns - I see lots on information on how to get the datakey from a gridview row, but not the reverse.
View 4 RepliesI've created a Web User Control that is placed on the page at design time. It's purpose is to pop up with a grid of items the user is to choose from. So I've got a gridview on it. And this code in the usercontrol:
Public Property DataTable() As Data.DataTable
Get
If Not IsNothing(ViewState("_SelectGridDataTable")) Then
Return ViewState("_SelectGridDataTable")
Else
Return Nothing
End If
End Get
Set(ByVal value As Data.DataTable)
ViewState("_SelectGridDataTable") = value
End Set
End Property
So when the user clicks on a button on the page, I put this in the code on the page:
SelectGrid2.DataTable = GetContacts().Tables(0)
SelectGrid2.Show()
And this works fine. The grid pops up (using jQuery) and the grid is shown. The problem is, that when the user clicks the "OK" button on the UserControl, the ViewState("_SelectGridDataTable") is always nothing. And I don't get it, since it's in the viewstate.. ?!? So does viewstate here not REALLY mean viewstate?
My head aches from beating it against the wall for so long. I have a gridview that is populated from a code behind table. The gridview has 3 bound lables, a checkbox (unbound) and another label (unbound). All fields are in template fields. I have coded a vb sub with one command: beep. It fires perfectly every time a user checks the gridview ckeckbox. Here are the columns of the gridview:
1. Player - bound label
2. Description- bound label
3. Amount- bound label
4. ToPay - ckeckbox
5. AmountToPay - unbound label
When a gridviewrow's checkbox is checked (or unchecked) the code determines the check status. If it is checked then the AmountToPay label is to display the same value of the third label (Amount) or it will clear it's text value if the checkbox is unchecking the box. Here is my code:
The gridview:
[Code]....
Here is the vb code that does beep():
[Code]....
I have tried about a dozen ways of reading the checked property of the checkbox but nothing works (sender, e, gridview, rows, items, cells, findcontrol, adnauseum). I want to work the code like this somehow:
If row.Checkbox.checkproperty=true then
set lblToBePaid.Text = the lblAmount.Text
Else
lblToBePaid.Text = ""
EndIf
What is the equivalent of gridviewrow to datagrid??
View 2 RepliesI would like that ID of subcontrol generated from the GridViewRow (NamingContainer) be named including database unique ID.
Exemple :
Suppose in the first colum of my GridView I have a Label displaying the name of an item fetch from a database table.
SQL Database table definition
SQLTable
- UniqueID
- Name
In this table I have the following records
- 1956 Hello
- 3013 World
- ... ...
Suppose I have a GridView having a label (id="lbName") in the first column template.
Now I'm binding GridView using this query : SELECT UniqueID, Name FROM SQLTable
After binding, the HTML result will be :
myGridView_ctl101_lbName
But I would like that NamingContainer names sub items like this :
myGridView_ctl1956_lbName
So I have tried in the RowDataBound event of the GridView : e.Row.NamingContainer.ID = (DataRowView)e.Row.DataItem)["UniqueID"].ToString()At first look it output exactly what I need but after that I can't get working Edit mode, it seem to break DataKeys or something else.
I add gridview row dynamically like that; After page postback some gridrow change and not visible.
protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow gvr = e.Row;[code]....
i want to add b.colr to gridview row
html code
<div>
Is there an easier way to erase the data from all controls in a gridviewrow than my current approach (code below)?
The problem: When a new type of control is added to the grid, like a radiobutton, it would not get handled here without adding to the code. The tests for type of control can get long and monotonous.
The solution I am seeking: Test the control once for "is WebControl". If it is a web control, cast it and call a single command that would erase its data. Does such a concept exist in ASP.NET 2.0/C#? I know I can do other things on the WebControl level, like set backcolor, but would like to do the same for erasing data.
foreach(Control c in row.Controls)
{
if(c is TextBox)
{
TextBox tb = (TextBox)c;
tb.Text = string.Empty;
}
elseif(c is CheckBox)
{
CheckBox cbx = (CheckBox)c;
cbx.Checked = false;
}
elseif(c is DropDownList)
{
DropDownList dd = (DropDownList)c;
dd.ClearSelection();
}
}
I tried translating this from vb but getting the error on e.Row.DataItem("CustomerID").ToString i tried varios things but no gooddbSrc.SelectCommand = "SELECT * FROM Orders WHERE CustomerID = '" + e.Row.DataItem("CustomerID").ToString + "' ORDER BY OrderDate";
View 3 RepliesIf in the coding before gridview databind, I have set the dataset ds to the gridview datasource.
And dataset ds which has more columns than columns in gridview shown and this gridview has checkbox in the first column.
If I want to get the value which is in the dataset ds which refer to the gridview with checkbox checked.
it should be what syntax?
grview["Data10"]?
I cannot use grview.cells, as these datas not shown in the gridview but refer to the gridview datasource dataset values.
We have a ASP.net form [.NET 3.5 on IIS 6] that loads controls dynamically. We are able to retain the values in the viewstate as long as the postback happens within 20 minutes. The database also gets updated properly. Everything works as expected.
However, If it takes more than 20 minutes for a user to fill out the form, the controls no longer retain their values during postbacks. The session values are intact, the user authentication is also intact. We tried several things
1) Added machine keys to web.config files - we have 2 web servers load balanced by Windows load balancer
2) We confirmed that the user are routed to the same server - because the sessions are sticky
3) Increased the session timeout to 60 minutes in IIS 6.0
4) Increased the Idle timeout for connection pool to 60 minutes
5) Changed Form authentication ticket timeout to 60 minutes
Server Error in '/EBOOKS' Application.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source Error:
Line 61: Dim favorname As String = GridView1.DataKeys(row.RowIndex).ToStringLine 62: Line 63: ' Pass the value of the selected Employye ID to the Delete //command. My coding as
Protected Sub btnMultipleRowDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMultipleRowDelete.Click
' Looping through all the rows in the GridView
For Each row As GridViewRow In GridView1.Rows
Dim checkbox As CheckBox = CType(row.FindControl("cbRows"), CheckBox)
'Check if the checkbox is checked.
'value in the HtmlInputCheckBox's Value property is set as the //value of the delete command's parameter.
If checkbox.Checked Then
' Retreive the Favorname
Dim favorname As String = GridView1.DataKeys(row.RowIndex).ToString
' Pass the value of the selected Employye ID to the Delete //command.
SqlDataSource1.DeleteParameters("Favorname").DefaultValue = favorname.ToString()
SqlDataSource1.Delete()
End If
Next row
End Sub
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
Width="563px" AutoGenerateColumns="False">
<RowStyle CssClass="fontsmallthennormal" />
<HeaderStyle CssClass="fontnormalblue8" />
<EditRowStyle CssClass="fontsmallthennormal" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbRows" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Favorname" HeaderText="Favorname" SortExpression="Favorname" />
<asp:BoundField DataField="FavorDate" HeaderText="FavorDate" SortExpression="FavorDate" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:yyyy %>"
runat="server" SelectCommand="SELECT [US], [Favorname], [FavorDate] FROM [TBL_FAVORITES] WHERE ([US] = @US)"
DeleteCommand="DELETE from TBL_FAVORITES where [Favorname]= @Favorname"
>
<SelectParameters >
<asp:ControlParameter ControlID="txtusername1" Name="US" PropertyName="Text" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="Favorname" />
</DeleteParameters>
</asp:SqlDataSource>
I've got a FormView bound to an ObjectDataSource. The FormView/ODS selects and displays my data correctly but when I change the data and try to update,nothing (seemingly) happens.Stepping through the code and digging deeper into the stack trace and output window,I figured out that the following sequence of events happens:
- The ObjectDataSource drills down to System.Web.UI.WebControls.ObjectDataSourceView.BuildDataObject to create the object I specified in the "DataObjectTypeName" property
[code]...
- Here is where the IndexOutOfRangeException happens. Sometimes one or two of the data object's properties will be set, sometimes none. Either way, the exception is thrown and the sequence is stopped (the business object's update is never called).
- The exception is not thrown inside any of the properties' set methods.I'm assuming that the exception is happening when BuildDataObject is using Reflection to find my data object's properties (GetProperty() etc.. ). Since I can't see the source code of BuildDataObject and the debugger won't give me any more information, there is no way for me to see which property (if any) is causing this.I double checked the names and types of all 27 of the data object's properties.
[URL]
Which is pretty much eaxactly what I was after! . I have been trying to alter the edit functionality instead of actually having to click on an edit button the user can click anywhere on a selected row to bring up the edit popup window. I have been trying to figure this out for a couple of days and cannot find a way to get it going.
A dataview is throwing an out of range exception when a page submit button is clicked. The strange part is if you change the data in the dataview by selecting a different person in the DDL the page works fine. Its only when the page loads and the default person in the DDL is when the DataView throws the exception.
The exception is thrown on the last line when I add DV.Rows(0).Cells(1).Text to the id_num parameter. I know there is a value in the dataview because it is displayed on the page. I can refresh the dataview with another person's information and the code works fine. This really has me confused. Here is a bit of the code behind.
Dim dashDataSource
As
New
SqlDataSource()
dashDataSource.ConnectionString =
ConfigurationManager.ConnectionStrings("SSSConnectionString2").ToString()
dashDataSource.InsertCommandType =
SqlDataSourceCommandType.Text
dashDataSource.InsertCommand =
"INSERT INTO tblAcadReferrals(all columns and parameter info here)"
dashDataSourceInsertParameters.Add("id_num", DV.Rows(0).Cells(1).Text)
I have Gridview in UpdatePanal but their is problem with template field
<asp:TemplateField HeaderText="EDIT">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" ToolTip="Edit Unit" OnClick="lnkEdit_Click"
CausesValidation="False"><img src="IMG/screw-driver.png" alt="" /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and link button click event is
protected void lnkEdit_Click(object sender, EventArgs e)
{
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
ScriptManager sm = (ScriptManager)Page.Master.FindControl("smanager");
sm.RegisterPostBackControl(clickedRow);
lblUnitID.Text = clickedRow.Cells[0].Text;
txtUnitName.Text = clickedRow.Cells[1].Text;
txtDecimal.Text = clickedRow.Cells[2].Text;
btnUnit.Text = "Update";
}
but text box value is not set but when we do it in PostBackTrigger is work.
can we do this AsyncPostBackTrigger.
How can I manually populate a listview control without sql connection?
View 3 RepliesI want to insert an extra row in the gridview in the RowDataBound event (or another place if thats possible). How can I do that?
The data in the row to be inserted is not part of the resultset, but rather to split the gridview in logical sections.
I got a data like this :
ID
Url
1
c:a.jpg;c:.jpg
when I bind it into gridview, i want it to be like this:
ID
Url
1
a.jpg
how can I create a datalist manually, and bind it to my data source by code not by wizard
I use ASP.Net, Using C#.Net , and SQL Server Data base
I don't want to databind a gridview, I want to be able to add rows myself within c# code. I can see there is a GridViewRow object and wondered if I can use this to add/remove rows as required?
View 5 Replies