Passing a values from Datatable to UserControl Property - asp.net

I am new to programming. I'm creating a program which adds a UserControl to a Gridview template dynamically and I want to pass a value from datatable to a property of a UserControl. The TemplateField in Gridview is already preloaded with 1 user control and if I clicked on a button, this will add a new UserControl with a numbering.
Here is the code from the page on FormLoad event.
Dim dtSESRatings As New DataTable
dtSESRatings.Columns.Add("IncrementNumber")
dtSESRatings.Rows.Add("1")
ViewState("dtSESRatings") = dtSESRatings
gvSesRating.DataSource = dtSESRatings
gvSesRating.DataBind()
And I want to add the row to the Numbering label in the user control which is the IncrementNumber Property set on SESRatings.ascx.
This is the asp code:
<asp:GridView ID="gvSesRating" runat="server" Width="100%" ShowHeader="false" GridLines="None"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<ses:SESRatings runat="server" ID="ses1" IncrementNumber="1" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
After FormLoad, upon clicking on a button "ADD NEW RATING", it is supposed to create a new user control in the gridview and the code is here:
Protected Sub btnAddRating_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddRating.Click
Dim dt As DataTable = ViewState("dtSESRatings")
Dim dtCnt As Integer = dt.Rows.Count
dtCnt += 1
dt.Rows.Add(dtCnt)
ViewState("dtSESRatings") = dt
gvSesRating.DataSource = dt
gvSesRating.DataBind()
dt = Nothing
End Sub
I want that the user control that will be added in the GridView will get the dtCnt and pass it to the IncrementNumber Property of the User Control. How can I do that? Please help. Thanks.

you must use Eval tags in your gridview control:
<ses:SESRatings runat="server" ID="ses1" IncrementNumber='<%# Eval("IncrementNumber") %>' />

Related

Ajax CalendarExtender not displaying 2nd row on

I have a Ajax CalendarExtender, ID'd to a textbox and data sourced to more than one record.
If the textbox's are all blank, no assigned value. The textbox's will popup the calendar with click.
If the textbox's contain a value, the first textbox will pop a blank calendar and the rest will pop nothing.
I have stand-alone textbox's with the ajax calendar and they work fine. Here is what I have with assigned value.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:TemplateField HeaderText="Maturity Date">
<ItemTemplate >
<asp:TextBox ID="txtMatureDate" text="4" runat ="server"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="maturitydate" TargetControlID="txtMatureDate" Format="MMMM,yyyy" runat ="server" DefaultView="Months" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is datasource.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SqlDataSource3 As New SqlDataSource()
SqlDataSource3.ID = "SqlDataSource3"
Me.Page.Controls.Add(SqlDataSource3)
SqlDataSource3.SelectCommand = "select maturitydate from PFW_LPMCalc_test where maturitydate >= '02/01/2017' and maturitydate <= '07/31/2017' order by maturitydate desc"
SqlDataSource3.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("jdwholegoods").ConnectionString
GridView1.DataSource = SqlDataSource3,
GridView1.DataBind()
End Sub
I have scriptmanager in place and
Ok.. so I uninstalled the Ajax Toolkit and reinstalled. It had a newer version though I don't think that did it. My guess is something was on top of current version and it needed to be back on top.

Get row values programmatically from gridview

I have a GridView Control and one button:
<asp:GridView ID="grdView" runat ="server" AutoGenerateColumns ="false" >
<Columns>
<asp:TemplateField HeaderText ="Balance">
<ItemTemplate>
<%#Eval("Balance") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button runat="server" ID ="btn" Text ="test"/>
Then on load page, I populate the gridView with a list of agreements. on that list there are one field called "Balance":
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
Dim agreementManager As AgreementManager = New AgreementManager()
Dim lstBalances As List(Of Agreement) = agreementManager.GetByClientId(2)
grdView.DataSource = lstBalances
grdView.DataBind()
End Sub
Then it display me this after loaded:
I am trying to read programmatically one specific balance with:
Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
Dim value As String = grdView.Rows(1).Cells(0).Text
End Sub
But the "value" is empty.
What I am doing wrong?
I am working on mock a system that uses this way to read the values from a grid view, and this code works fine:
Dim balance As Decimal =
CType(grdApplyTransactionsAgreements.Rows(idx).Cells(BALANCE_CELLID).Text, Decimal)
this code is inside a button too.
Thanks!!
The .Text property can only be read after DataBinding from AutoGenerated columns and BoundField columns. But even if you could I would not recommend it since all you are getting is a string, not the original datatype.
Better read the values from the source lstBalances.
I bumped into an answer, I need to use BondField Control instead Template Field, now everything runs fine.
<asp:GridView ID="grdView" runat ="server" AutoGenerateColumns ="false" >
<Columns>
<asp:BoundField DataField ="Balance" HeaderText ="Balance"/>
</Columns>
</asp:GridView>

Call Sub Procedure using Gridview Column Imagefield

I have a Gridview column called "Path" and when the user clicks on it I want it to pass that value of the "Path" cell to a sub procedure to execute the following code,
process.start(Path)
Currently the table is filled via SQL query and the "Path" is clickable but it wants to take me to URL which won't work for my situation. I also tried the Hyperlink column as well but was having the same problem. Any guidance would be appreciated! Thanks in advance.
You could use a LinkButton instead of a HyperLink.
Set the CommandArgument on the LinkButton, you could use either the GridView.RowCommand event handler or a LinkButton.OnClick event handler to call your sub.
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" OnRowCommand="gridView_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Path">
<ItemTemplate>
<asp:LinkButton ID="lbPath" runat="server" CommandArgument="<%# Eval("Path")%>" OnClick="lbPath_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then in code behind:
Protected Sub lbPath_Click(sender As Object, e As EventArgs)
Dim linkButton As LinkButton = CType(sender, LinkButton)
Process.Start(linkButton.CommandArgument)
End Sub
For reference
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.110).aspx

Update Hyperlink In Gridview not working

Gridview is not working when user click on the link. User is supposed to be directed to another page when clicked on the link and on the second page, there is a text box that will allow user to update.
this is my code for grid view:
<asp:BoundField DataField = "status" HeaderText = "Status" HtmlEncode = "true" ItemStyle-Width="150px" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:ButtonField ButtonType="Link" Text="Click" CommandName="Select" HeaderText="Details" />
<asp:ButtonField HeaderText="Update" Text="update?" CommandName="GridView1_RowUpdated" />
code behind:
Protected Sub GridView1_RowUpdated(sender As Object, e As GridViewUpdatedEventArgs)
Dim cr_number As String = GridView1.SelectedRow.Cells(0).Text
Response.Redirect("upddetail.aspx?id=" + cr_number)
End Sub
since you are set the CommandName as GridView1_RowUpdated you can use GridView1_RowCommand event as below
Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "GridView1_RowUpdated" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim cr_number As String = GridView1.Rows(index).Cells(0).Text
Response.Redirect("upddetail.aspx?id=" + cr_number)
End If
End Sub
in your aspx page set onrowcommand as below
<asp:gridview id="GridView" onrowcommand="GridView1_RowCommand"
instead of all above you can use hyperlinkfield
<asp:hyperlinkfield text="Update?"
datanavigateurlfields="Id"
datanavigateurlformatstring="~\upddetail.aspx?id={0}"
headertext="Update"
target="_blank" />
You are using ButtonField not an actual hyperlink. When user clicks on this there will be a postback to your original page and it will not navigate to another page. Try using HyperLink.

ASP.NET - GridView - How to programmatically add and read new controls to the cell?

Here is the setup:
I programmatically populate my gridview using LINQ to SQL query. Then I enter the edit mode and want to replace some standard TextBox controls with DropDownLists like this:
'excerpt from GridView1_RowEditing
Dim ddlist1 As New DropDownList
Dim res1 = From items1 In mydb.Items
Select items1.Col10
ddlist1.DataSource = res1
ddlist1.DataBind()
GridView1.Rows.Item(0).Cells(1).Controls.Add(ddlist1)
At this moment I have my gridview showing standard textbox control and new DDList control (in the Column 1).
The problem is -- I cant read the value from DDList in the RowUpdating method. It seems like DDList control is not in the GridView1.Rows.Item(0).Cells(1).Controls collection.
RowUpdating method sees only standard TextBox control and can read it's value.
Any suggestions are welcome. I just don't understand something here :(
Use TemplateField, and then you can locate your dropdown using FindControl
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
...
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim ddl = CType(GridView1.Rows(e.RowIndex).Cells(1).FindControl("DropDownList1"), DropDownList)
End Sub

Resources