We have an ASP.Net / VB.Net code-behind web form. On this form is a GridView and a DetailsView.
When there is a change in the details view we would like the changes to be reflected in the GridView.
This code is being used to populate the GridView:
Private Sub Teachers_Init(sender As Object, e As EventArgs) Handles Me.Init
' Load the data from the database into the GridView.
'---------------------------------------------------
GridViewSummary.DataSource = theTableAdapter.GetDataAllTeachers
GridViewSummary.DataBind()
End Sub
This coding is being used to reflect the changes in the GridView after a change to the details are made in the DetailsView:
Private Sub DetailsView_ItemUpdated(sender As Object, e As DetailsViewUpdatedEventArgs) Handles DetailsView.ItemUpdated
' Refresh the data so current values are displayed.
'--------------------------------------------------
GridViewSummary.DataSource = theTableAdapter.GetDataAllTeachers
GridViewSummary.DataBind()
End Sub
Is there a way to not need the use of:
GridViewSummary.DataSource = theTableAdapter.GetDataAllTeachers
in the above coding? I know it's just 1 line of code but would like to eliminate it if it's not needed.
Related
I need to access a label control after the edit is clicked to bindgrid based on the label text. How do I do that?
Private Sub ActionItems_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles ActionItems.EditCommand
ActionItems.EditItemIndex = e.Item.ItemIndex
Dim fieldtypelbl As Label = e.Item.FindControl("lblrcause")
FillActions(fieldtypelbl.text)
End Sub
I was able to resolve the same by using
source.Parent.fieldname
as the datagrid is a part of the control being called on the same page multiple times
I have a page with GridView. The GridView has select button. Normally I use GridView's selected index changed event to do all kinds of operations when user clicks select button. But now I want to do some operations in Page_Load event based on grid view's selected row. Since the Selected_Index_changed event occurs after Page_Load how do I know following things in page load event.
I checked the asp lifecycle and this other question but I dont know how to do this.
How about using a QueryString to transmit which row was selected and then in the Page_Load event get the QueryString parameters? This is an example.
Protected Sub LinkButton1_Command(sender As Object, e As CommandEventArgs)
Dim UserId As Integer = e.CommandArgument 'Here goes whatever value you're trying to pass
Response.Redirect("~/OtherPage.aspx?UserId=" _
& UserId)
End Sub
This is in the OtherPage.aspx
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
UserId = Request.QueryString("UserId")
'Your code
end sub
I have a checkbox and a panel inside of a FormView control, and I need to access them from the code behind in order to use the checkbox to determine whether or not the panel is visible. This is the code that I originally used, but since I put the controls inside of the FormView, it no longer works.
Protected Sub checkGenEd_CheckedChanged(ByVal sender As Object, _
ByVal e As System.EventArgs)
If checkGenEd.Checked = True Then
panelOutcome.Visible = True
Else
panelOutcome.Visible = False
End If
End Sub
I've started to figure this out based on other questions I looked up on here, but all of them were in C# instead of VB, so this is as far as I got:
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Edit Then
End If
End Sub
So yeah I'm not sure exactly how to finish it. I'm sorry, this might be pretty basic, but I'm new at this and any help would be appreciated!
EDIT: here's my code now:
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Edit Then
CheckBox checkGenEd = formview1.FindControl("checkGenEd");
Panel panelOutcome = formview1.FindControl("panelOutcome");
End If
End Sub
It's also saying that checkGenEd and panelOutcome are not declared.
EDIT: I changed my code to this but it still doesn't work:
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Edit Then
Dim checkGenEd As CheckBox = FormView1.FindControl("checkGenEd")
Dim panelOutcome As Panel = FormView1.FindControl("panelOutcome")
If checkGenEd.Checked = True Then
panelOutcome.Visible = True
Else
panelOutcome.Visible = False
End If
End If
End Sub
There aren't any errors anymore, but nothing happens when I click the checkbox. I think there needs to be some kind of event to trigger it but I don't know how you can put an event handler inside of an event handler.
With FormView, you have to use find control, as in:
CheckBox checkGenEd = (CheckBox)formview1.FindControl("checkGenEd");
Panel panelOutcome = (Panel)formview1.FindControl("panelOutcome");
You cannot reference a control directly by ID.
HTH.
In VB you need use Directcast
Dim chk As Checkbox = DirectCast(Me.FormView1.FindControl("checkgen"), Checkbox)
FormView has its own event framework. A normal control within a FormView won't generate the postback events you are looking for. I initially made the same mistake. I wanted, like you, to generate some kind of postback that could be intercepted at the server end. Once we get back to the server we can look at the values in checkboxes etc depending on whatever business rules apply. This is what I did.
First of all put all relevant controls within an
<EditItemTemplate>
section within the FormView. (There are other Template tags that may be more appropriate). To generate the postback have a button (for example) like the one below. (This has to be within the EditItemTemplate section as well):
<asp:linkbutton id="UpdateButton"
text="Update"
commandname="Update"
runat="server"/>
You can intercept this at the server with the FormView event ItemCommand. For example:
Private Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
'your code here
End Sub
Once you are back at the server you can then start looking at the various controls to see what they hold, using findControl if necessary. The button command shown above is an example so you might want to use another control.
I creat one WEB project, this project contain tow WEB FORM, In the first Web Form Design i have tow TextBox for Entring the date(All dataTable between this tow dates) and one Button, I want that when i press to to this Button it will load the second WEB FORM and show all the Data Table in DataGrid In this WEB FORM, So i need To call this tow TextBox value from the first WEB FORM to the second WEB FORM In Load_Page i will use this tow value in select statment. So i want to know how to call this to value from the first WEB FORM. I'am using VB.NET WEB APPLICATION.i have allrady DB in SQL .
you have a lot of ways to pass values from page to another like query string, sessions, etc....
Another way to achieve this is by using the PostBackUrl property of the button.
Refer Button..::.PostBackUrl Property for more information
You can use querystring to get last page values to load page. Use this code
Response.Redirect("Default.aspx?value1 =" & value & "&value2" & value)
write page name as like default.aspx.
In the button click on the first page, you can call the second page by using Server.Transfer like this:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
TextBox1.Text = TextBox1.Text & " Add something before continue..."
Server.Transfer("SecondPage.aspx")
End Sub
Then you can get the textbox from the first page in the second page Page_load event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lastPage As Page = Context.Handler
Dim textBox As TextBox = lastPage.FindControl("TextBox1")
End Sub
If you don't need to do anything on the button event other than posting to the next page, you can do it simpler...
Definition of the button in the ASPX markup for the first page:
<asp:Button id="Button1" PostBackUrl="SecondPage.aspx" Text="Continue" runat="Server"/>
Then you can get the textbox from the first page in the second page Page_load event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim textBox As TextBox = PreviousPage.FindControl("TextBox1")
End Sub
I have the latest release of the AjaxControlToolkit, and am using an AjaxControlToolkit Html Editor control inside an AjaxControlToolkit ReorderList. I need to be able to access the "content" property so I can write the value to the database but FindControl doesn't find it.
for instance, I can find a TextBox control inside the ItemCommandEvent:
Protected Sub IncludedSectionComponentsReorderList_ItemCommand(ByVal sender As Object, ByVal e As AjaxControlToolkit.ReorderListCommandEventArgs) Handles IncludedSectionComponentsReorderList.ItemCommand
If e.CommandName = "SaveChanges" Then
Dim txtSectionComponentLabel As TextBox = CType(e.Item.FindControl("txtSectionComponentLabel"), TextBox)
End If End Sub
But I can't find an AjaxControlToolkit Html Editor in the same event:
Protected Sub IncludedSectionComponentsReorderList_ItemCommand(ByVal sender As Object, ByVal e As AjaxControlToolkit.ReorderListCommandEventArgs) Handles IncludedSectionComponentsReorderList.ItemCommand
If e.CommandName = "SaveChanges" Then
Dim editor As Editor = CType(e.Item.FindControl("3Editor"), Editor)
strSectionControlValue = editor.Content
End If
End Sub
I haven't used the AjaxControlToolKit ReorderList control before, but you may need to access something like the "_OnItemDataBound" event. Sometimes controls are shoved in there when a row is being databound and you can't access them as you would think.
Good luck and hope this helps some.
Here's a solution for you:
In this example it's taking content from an aspx page to an ascx page
AjaxControlToolkit.HTMLEditor.Editor txtNotes = (AjaxControlToolkit.HTMLEditor.Editor)Page.FindControl("txtNotes");
txtView.Content = txtNotes.Content;