asp.net Title Labels not populating from variables on page load - asp.net

I have declared variables and populated from dropdownlists in my asp. However on pageload the labels aren't populating, they only populate once I change values in the dropdowns. So the variables are working with the labels, just not initially on page load.
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim strDataCollection As String = ddlDataCollection.Text
Dim strYear As String = DdlYear.Text
Dim strSubject As String = DdlSubject.Text
Dim strTeachingGroup As String = DdlTeachingGroup.Text
Dim strSubgroup As String = ddlSubgroup.SelectedItem.Text
lblHeaderYear.Text = "Year " & strYear
lblHeaderDataCollection.Text = " " & strDataCollection
lblHeaderSubject.Text = " " & strSubject
lblHeaderTeachingGroup.Text = " " & strTeachingGroup
lblHeaderSubroup.Text = " " & strSubgroup
If strTeachingGroup = "Select All" Then
lblHeaderTeachingGroup.Visible = False
Else
lblHeaderTeachingGroup.Visible = True
End If
If strSubgroup = "Select All" Then
lblHeaderSubroup.Visible = False
Else
lblHeaderSubroup.Visible = True
End If
End Sub
The asp for my labels is:
<div class="centeronpage">
<asp:Label ID="lblHeaderYear" runat="server" Text="" CssClass="header"></asp:Label>
<asp:Label ID="lblHeaderDataCollection" runat="server" Text="" CssClass="header"></asp:Label>
<asp:Label ID="lblHeaderSubject" runat="server" Text="" CssClass="header"></asp:Label>
<asp:Label ID="lblHeaderTeachingGroup" runat="server" Text="" CssClass="header"></asp:Label>
<asp:Label ID="lblHeaderSubroup" runat="server" Text="" CssClass="header"></asp:Label>
</div>
EDIT ANSWER
I created the following PreRender as suggested below. I then included OnPreRender="Page_PreRender" in one of the label elements and this seemed to trigger the prerender.
Protected Sub Page_PreRender(sender As Object, e As EventArgs)
Dim strDataCollection As String = ddlDataCollection.Text
Dim strYear As String = DdlYear.Text
Dim strSubject As String = DdlSubject.Text
Dim strTeachingGroup As String = DdlTeachingGroup.Text
Dim strSubgroup As String = ddlSubgroup.SelectedItem.Text
lblHeaderYear.Text = "Year " & strYear
lblHeaderDataCollection.Text = " " & strDataCollection
lblHeaderSubject.Text = " " & strSubject
lblHeaderTeachingGroup.Text = " " & strTeachingGroup
lblHeaderSubroup.Text = " " & strSubgroup
End Sub
EDIT - DDL Binding
<asp:Label ID="lblHeaderYear" runat="server" Text="" CssClass="header" OnPreRender="Page_PreRender"></asp:Label>
<asp:Label ID="lblHeaderDataCollection" runat="server" Text="" CssClass="header"></asp:Label>
<asp:Label ID="lblHeaderSubject" runat="server" Text="" CssClass="header"></asp:Label>
<asp:Label ID="lblHeaderTeachingGroup" runat="server" Text="" CssClass="header"></asp:Label>
<asp:Label ID="lblHeaderSubroup" runat="server" Text="" CssClass="header"></asp:Label>

I've not checked it, but I think the problem is as the Page_load time, there's actually no selectditem. I would suggest trying to bind the labels in the prerender event, where dropdowns should actually have selected items (the first one if you didn't specify anything else)
EDIT :
you should do that for your prerender event :
Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.Load
Dim strDataCollection As String = ddlDataCollection.Text
Dim strYear As String = DdlYear.Text
Dim strSubject As String = DdlSubject.Text
Dim strTeachingGroup As String = DdlTeachingGroup.Text
Dim strSubgroup As String = ddlSubgroup.SelectedItem.Text
lblHeaderYear.Text = "Year " & strYear
lblHeaderDataCollection.Text = " " & strDataCollection
lblHeaderSubject.Text = " " & strSubject
lblHeaderTeachingGroup.Text = " " & strTeachingGroup
lblHeaderSubroup.Text = " " & strSubgroup
End Sub
Note the "Handles Me.Load" after your PreRender event declaration. This is how you actually override the page's PreRender event , exactly as you do it for your PageLoad. You should then remove all prerender event on your labels in you aspx page.
Please note that you can also make use of the AutoEventWireup Page attribute :
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
This will allow you, for Page events only, to tell .NET to automatically override page events if a method with the same name is defined in your codebehind. In other words, you'd be able to remove the "Handles Me.xxx" after event declaration.

Related

how to get all values of selected checkbox in vb.net and set it on string list

I need to get selected items on checkbox and set as string format like (value1,value2,value3) from the checkbox i selected
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim CheckRow As CheckBox = (TryCast(row.Cells(1).FindControl("chckSelector"), CheckBox))
If CheckRow.Checked Then
Dim scode As String = TryCast(row.Cells(2).FindControl("lblsstorecode"), Label).Text
lbltest.Text = 'this i want to get the value like this (value1,value2,value3) from checkbox that i selected
End If
End If
Next
Depending on why you are using the GridView control, you might me able to accomplish this easier by using a CheckBoxList instead. In Asp.net what you describe is accomplished very easily with a CheckBoxList as follows:
In .aspx:
<asp:CheckBoxList ID="Itemlst" runat="server" RepeatColumns="1">
<asp:ListItem Value="">Item 1</asp:ListItem>
<asp:ListItem Value="">Item 2</asp:ListItem>
<asp:ListItem Value="">Item 3</asp:ListItem>
</asp:CheckBoxList>
</br>
<asp:Button ID="Button1" runat="server" Text="Button" />
</br>
<asp:TextBox ID="TextBox1" runat="server" Width="400"></asp:TextBox>
Then in the code behind:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim selected = New List(Of ListItem)
Dim itemcount As Integer
Dim csvlist As String = ""
For Each item As ListItem In Itemlst.Items
If item.Selected Then selected.Add(item)
Next
itemcount = selected.Count
For i = 0 To itemcount - 1
csvlist = csvlist & selected(i).ToString & ","
Next
csvlist = Mid(csvlist, 1, Len(csvlist) - 1)
TextBox1.Text = csvlist
End Sub
The Windows Forms CheckedListBox would probably work similar if you are developing a desktop application.

Child gridview update event

I have problem regarding child gridview (gridview inside ListView), so far i solved the databind and the the Delete command , but i couldn't figure out the Edut Mode ( i click the image button) but the GV doesn't go to edit mode the codes are as follows: thanks in advance
Page mark-up:
EditItemTemplate
<asp:ImageButton ID="imgbUpdate" runat="server" CommandName="Update" Text="Update" ImageUrl="~/Images/save_icon_mono.gif" CausesValidation="true" ValidationGroup="vgrpSaveContact"/>
<asp:ImageButton ID="imgbCancel" runat="server" CommandName="Cancel" Text="Cancel" ImageUrl="~/Images/undo_icon_mono.gif" CausesValidation="false"/>
EditItemTemplate
<ItemTemplate>
<table>
<tr>
<td> <asp:ImageButton ID="imgbEdit" runat="server" CommandName="Edit" Text="Edit" ImageUrl="~/Images/edit_icon_mono.gif" />
</td>
<td> <asp:ImageButton ID="imgbDelete" runat="server" CommandName="Delete" Text="Delete" ImageUrl="~/Images/delete_icon_mono.gif" ToolTip="Delete"/>
</td>
</tr>
</table>
</ItemTemplate>
Code VB.NET
Protected Sub gvSorties_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim connString As String = ConfigurationManager.ConnectionStrings("MoyensAeriensConnectionString").ConnectionString
Dim gvTemp As GridView = DirectCast(sender, GridView)
gvUniqueID = gvTemp.UniqueID
'Get the value
Dim strSortieID As String = DirectCast(gvTemp.Rows(e.RowIndex).FindControl("lblSortieID"), Label).Text
Using conn As New SqlConnection
conn.ConnectionString = connString
conn.Open()
Try
Dim strSQL As String = ""
Dim dsTemp As New SqlDataSource()
dsTemp.ConnectionString = connString
'Get the values stored in the text boxes
Dim strMissionNo As String = DirectCast(gvTemp.Rows(e.RowIndex).FindControl("txtMissionNo"), TextBox).Text
Dim strCaptain As String = DirectCast(gvTemp.Rows(e.RowIndex).FindControl("txtCaptain"), TextBox).Text
Dim strCrew As String = DirectCast(gvTemp.Rows(e.RowIndex).FindControl("txtCrew"), TextBox).Text
Dim strFuel As String = DirectCast(gvTemp.Rows(e.RowIndex).FindControl("txtFuel"), TextBox).Text
'Prepare the Update Command of the DataSource control
strSQL = "UPDATE Sortie set MissionNo = '" & strMissionNo & "'" & ",Captain = " & strCaptain & "" & ",Crew = '" & strCrew & "'" & ",Fuel = '" & strFuel & "'" & " WHERE SortieID = " & strSortieID
dsTemp.UpdateCommand = strSQL
dsTemp.Update()
'Reset Edit Index
gvEditIndex = -1
lvODV.DataBind()
conn.Close()
Catch
End Try
End Using
End Sub
Many thanks to every one

Trouble with Listbox bind inside Gridview

I am a AS400 programmer asked to write a program in asp.net using vb.net. I have never done this before and I am having issues with populating a listbox in Gridview. I have researched this subject for days but all the code I have tried, found in examples, do not work. Please forgive any really bad code, with me being so new to .net, I am sure this could be written much better. I appreciate any help you may be able to offer. the grid is "AdjusterList" and the Listbox is called "MAICD". I think the 'failing/bad' code in
Public Sub AdjusterList_RowDataBound. It is giving me a null exception error the line before the databind; oCtrl.DataSource = oRs
I am using code given to me by a senior .net programmer, whom is not able to offer any more assistance to this newbie. Just Fyi....
Here is my aspx.
<div id="div1" runat="server">
<asp:GridView ID="AdjusterList" runat="server" Width="1100px"
ClientIDMode="Static" AllowSorting="True"
AutoGenerateColumns="False"
OnRowCommand="AdjusterList_RowCommand"
OnRowEditing="AdjusterList_RowEditing"
OnRowUpdating="AdjusterList_RowUpdating"
OnRowDeleting="AdjusterList_RowDeleting"
OnRowCancelingEdit="AdjusterList_RowCancelingEdit"
OnRowDataBound="AdjusterList_RowDataBound"
DataKeyNames="INSCD, INSSEQ"
ShowHeaderWhenEmpty="True" EditRowStyle-BackColor="#FF9900" PageSize="20"
EmptyDataText="NO RECORDS FOUND FOR THIS INSURER"
ShowFooter="True"EnableViewState="true">
<EditRowStyle BackColor="#FF9900" />
<RowStyle BackColor="White" ForeColor="Black" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton id="btnedit" runat="server" CommandName="Edit"/>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton id="btnupdate" runat="server" CommandName="Update" Text="Save" />
<asp:LinkButton id="btncancel" runat="server" CommandName="Cancel" Text=Cancel"/>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton id="btninsert" runat="server" CommandName="Insert" Text="Insert"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Mail Code" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblmaicd" runat="server" Text='<%# Bind("MAICD")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:ListBox ID="MAICD" runat="server" Rows="1"DataTextField="Text"
DataValueField='<%# Bind("MAICD")%></asp:ListBox>
</EditItemTemplate>
<FooterTemplate>
<asp:ListBox ID="MAICD" runat="server" width="200" DataTextField="Text">
</asp:ListBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
My code behind...
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Class EditAdjusterData
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e AsSystem.EventArgs)
Dim sAdjuster As String = Session("Adjuster")
Dim sSeqNo As String = Session("SeqNo")
PopulateEdit(sAdjuster, sSeqNo, dateok)
If IsPostBack Then
'divEdit.Visible = False
'divSelect.Visible = True
'ShowForm()
Else
AdjusterList.DataBind()
'divEdit.Visible = True
'divSelect.Visible = False
End If
End Sub
Protected Sub PopulateEdit(sValue As String, sValue2 As String, sValue3 As String)
' CALL TO PGRTSTLIB FILE INSP TO GET INSURER NAME
' 2ND CALL TO PGRTSTLIB FILE INSD
Dim oConn As New OleDbConnection()
Dim sConn As String = ""
Dim oCmd As New OleDbCommand()
Dim oAdapter As New OleDbDataAdapter()
Dim oRs As DataSet = New DataSet
Dim sSql As String = ""
sConn = "Provider=IBMDA400.DataSource.1;
oConn = New OleDb.OleDbConnection(sConn)
oConn.Open()
sSql = "SELECT INSCD, INSSEQ, MAICD, MAISEQ, (substr(char(EFFDT),5,2) || '-' || substr(char(EFFDT),7,2) || '-' || substr(char(EFFDT),1,4)) AS EFFDT, (substr(char(CANDT),5,2) || '-' || substr(char(CANDT),7,2) || '-' || substr(char(CANDT),1,4)) AS CANDT, ACCFIL FROM PGRTSTLIB.INSD WHERE INSCD = '" & sValue & "' "
oCmd = New OleDbCommand(sSql, oConn)
Session(INSCD) = INSCD
Session(EFFDT) = EFFDT
Session(MAICD) = MAICD
Session("CANDT") = CANDT
oCmd.CommandType = CommandType.Text
oAdapter.SelectCommand = oCmd
oAdapter.Fill(oRs, "Data")
Dim sMailCode As String = " "
Dim sMailSeq As String = " "
Dim pRow As DataRow
For Each pRow In oRs.Tables("Data").Rows
sMailCode = pRow("MAICD").ToString()
Next
Session(sMailCode) = MAICD
Session(MAICD) = sMailCode
Session(sMailCode) = sMailCode
AdjusterList.DataSource = oRs
'AdjusterList.DataBind()
'If sMailCode <> " " Then
' PopulateMailCode(sMailCode)
'End If
oRs.Dispose()
oAdapter.Dispose()
oCmd.Dispose()
oConn.Dispose()
End Sub
Protected Sub AdjusterList_PageIndexChanging(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
AdjusterList.PageIndex = e.NewPageIndex
AdjusterList.DataBind()
End Sub
Protected Sub AdjusterList_RowCommand(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
End Sub
Protected Sub AdjusterList_RowCreated(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
End Sub
Public Sub AdjusterList_RowDataBound(ByVal Sender As Object,
ByVal e As GridViewRowEventArgs) Handles AdjusterList.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.FindControl("MAICD") IsNot Nothing Then
Dim MAICD As ListBox = e.Row.FindControl("MAICD")
'If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
BindAjusterList()
End If
End If
End Sub
Public Sub BindAjusterList()
Dim oConn As New OleDbConnection()
Dim sConn As String = ""
Dim oCmd As New OleDbCommand()
Dim oAdapter As New OleDbDataAdapter()
Dim oRs As DataSet = New DataSet
Dim sSql As String = ""
sConn = "Provider=IBMDA400.DataSource.1; "
oConn = New OleDb.OleDbConnection(sConn)
oConn.Open()
'sSql = "SELECT MAICD as Value, MAICD AS Text from PGRTSTLIB.INSM order by MAICD"
sSql = "SELECT MAICD As Value, MAICD AS TEXT from PGRTSTLIB.INSM"
oCmd = New OleDbCommand(sSql, oConn)
'oCmd.Parameters.Add(New SqlParameter("#Type", Insurer))
oCmd.CommandType = CommandType.Text
oAdapter.SelectCommand = oCmd
oAdapter.Fill(oRs, "ListBox")
Dim oCtrl As ListBox
oCtrl = AdjusterList.FindControl("MAICD")
oCtrl.Items.Add(New ListItem("", ""))
oCtrl.DataSource = oRs
oCtrl.DataBind()
oCtrl.Items.Insert(0, New ListItem(String.Empty, String.Empty))
If Len(sValue) > 0 Then
oCtrl.SelectedValue = sValue
Else
sValue = " "
End If
oRs.Dispose()
oAdapter.Dispose()
oCmd.Dispose()
oConn.Dispose()
End Sub
Public Sub AdjusterList_RowEditing(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
AdjusterList.EditIndex = e.NewEditIndex
AdjusterList.DataBind()
End Sub
Protected Sub AdjusterList_RowUpdating(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
' Dim row As GridViewRow = DirectCast(SubsidaryList.Rows(e.RowIndex), GridViewRow)
Dim INSCD As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells (0).FindControl ("INSCD"), TextBox).Text
Dim INSSEQ As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(1).FindControl("INSSEQ"), TextBox).Text
Dim MAICD As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(2).FindControl("MAICD"), ListBox).Text
Dim MAISEQ As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(3).FindControl("MAISEQ"), TextBox).Text
Dim EFFDT As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(4).FindControl("EFFDT"), TextBox).Text
Dim CANDT As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(5).FindControl("CANDT"), TextBox).Text
Dim ACCFIL As String = DirectCast(AdjusterList.Rows(e.RowIndex).Cells(6).FindControl("ACCFIL"), TextBox).Text
AdjusterList.EditIndex = -1
AdjusterList.DataBind()
' New Data to DataBind to sql datasource and resend page after RECORD updated
Response.Redirect("EditAdjusterData.aspx")
End Sub
Protected Sub AdjusterList_RowCancelingEdit() Handles AdjusterList.RowCancelingEdit
AdjusterList.EditIndex = -1
AdjusterList.DataBind()
End Sub
End Class
I am so new to .net and I may not fully understand every example. I think the senior .net programmers think it is funny : (
Thanks so much and please feel free to contact me with any help you may be able to offer. I just need the darn listbox to populate. I removed any code I considered unrelated to this listbox issue.
In your markup, you should change the DataValueField property of your ListBox to the "Value" string, because it's the alias you are giving to the property in the query ("SELECT MAICD As Value, MAICD AS TEXT from PGRTSTLIB.INSM"):
<asp:ListBox ID="MAICD" runat="server" Rows="1"DataTextField="Text" DataValueField="Value"></asp:ListBox>
Also, in RowDataBound event, you are retrieving the ListBox instance for that row using the FindControl method, but then you are retrieving it again in the BindAjusterList() method. Try changing the method to receive the control found:
Public Sub AdjusterList_RowDataBound(ByVal Sender As Object, ByVal e As GridViewRowEventArgs) Handles AdjusterList.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.FindControl("MAICD") IsNot Nothing Then
Dim MAICD As ListBox = e.Row.FindControl("MAICD")
BindAjusterList(MAICD)
End If
End If
End Sub
Public Sub BindAjusterList(ByVal oCtrl As ListBox)
' existing logic
oCtrl.Items.Add(New ListItem("", ""))
oCtrl.DataSource = oRs
oCtrl.DataBind()
' existing logic
End Sub

DropDownList is not posting back

I'm using Visual Studio 2005 and ASP. NET 2.0 with this program. This is written in VB.net
Right now, we've got a Gridview that is embedded and databound into another gridview. The 2nd Gridview is hidden by default and expanded by the user. Within the 2nd gridview are several DropDownLists. The problem is that these DropDownLists are not firing the OnSelectedIndexChanged functon nor AutoPostback whenever I change the selecteditem.
I'm curious as to why it is not firing. Thank you.
Gridview1:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim row As GridViewRow = e.Row
Dim strSort As String = String.Empty
Dim strCNRNum As String
' Make sure we aren't in header/footer rows
If row.DataItem Is Nothing Then
Return
End If
' Find Child GridView control
Dim gv As GridView = New GridView()
gv = row.FindControl("GridView2")
If gv.UniqueID = gvUniqueID Then
gv.EditIndex = gvEditIndex
ClientScript.RegisterStartupScript(GetType(Page), "Expand", "<SCRIPT LANGUAGE='javascript'>expandcollapse('div" + e.Row.DataItem("CNR_NUM").ToString() & "','one');</script>")
End If
' Prepare the query for Child GridView by passing
' the Customer ID of the parent row
'Dim dsTemp As SqlDataSource
'dsTemp = ChildDataSource(e.Row.DataItem("CNR_NUM").ToString, strSort)
'gv.DataSource = dsTemp
'gv.DataBind()
strCNRNum = e.Row.DataItem("CNR_NUM").ToString()
Dim dt As DataTable = New DataTable()
Dim con As SqlConnection = New SqlConnection(ConnectionString)
Try
con.Open()
Dim strSQL As String
strSQL = "select " & _
"CS.SID, " & _
"CS.CNR_NUM, " & _
"CS.STEP_NUM, " & _
"CS.SERVER_ID, " & _
"S.SERVER_NM, " & _
"CS.DATABASE_ID, " & _
"D.DATABASE_NM, " & _
"CS.CMD_FILE_NM, " & _
"CS.EXECUTE_DTTM, " & _
"CS.STEP_TYPE_ID, " & _
"ST.STEP_TYPE, " & _
"ISNULL(CS.LOG_FILE_NM,'') as LOG_FILE_NM " & _
"from " & _
"dbo.CNR_STEPS CS INNER JOIN dbo.CNR_SERVERS S on CS.SERVER_ID = S.SERVER_ID " & _
"INNER JOIN dbo.CNR_DATABASES D ON CS.DATABASE_ID = D.DATABASE_ID " & _
"INNER JOIN dbo.CNR_STEP_TYPES ST ON CS.STEP_TYPE_ID = ST.STEP_TYPE_ID " & _
"where " & _
"CS.CNR_NUM = '" & strCNRNum & "' " & _
"order by " & _
"CS.STEP_NUM"
Dim cmd As SqlCommand = New SqlCommand(strSQL, con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
da.Fill(dt)
If dt.Rows.Count > 0 Then
gv.DataSource = dt
gv.DataBind()
Else
dt.Rows.Add(dt.NewRow())
dt.Rows(0)("CNR_NUM") = strCNRNum
dt.Rows(0)("LOG_FILE_NM") = ""
gv.DataSource = dt
gv.DataBind()
Dim colCount As Integer = gv.Columns.Count
gv.Rows(0).Cells.Clear()
gv.Rows(0).Cells.Add(New TableCell())
gv.Rows(0).Cells(0).ColumnSpan = colCount
gv.Rows(0).Cells(0).HorizontalAlign = HorizontalAlign.Center
gv.Rows(0).Cells(0).ForeColor = System.Drawing.Color.Red
gv.Rows(0).Cells(0).Font.Bold = True
gv.Rows(0).Cells(0).Text = "No Steps Defined"
End If
Catch ex As Exception
lblMessage.ForeColor = Drawing.Color.Red
lblMessage.Text = "Error: " & ex.Message.ToString()
'ClientScript.RegisterStartupScript(GetType(Page), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>")
End Try
End Sub
ASP code for the 2nd gridview, which is within the first one:
<asp:GridView ID="GridView2" AllowPaging="True" AllowSorting="true" Width="100%" Font-Size="Small" AutoGenerateColumns="false" runat="server" DataKeyNames="CNR_NUM" ShowFooter="true" OnRowEditing = "GridView2_RowEditing"
OnRowCommand = "GridView2_RowCommand"
OnRowDeleting = "GridView2_RowDeleting"
OnRowDeleted = "GridView2_RowDeleted"
OnRowUpdating = "GridView2_RowUpdating"
OnRowUpdated = "GridView2_RowUpdated"
OnRowCancelingEdit = "GridView2_CancelingEdit"
OnRowDatabound="GridView2_RowDataBound"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
>
<HeaderStyle Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Step Type">
<ItemTemplate><%#Eval("STEP_TYPE")%></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_StepTypes"
DataSourceID="ds_StepTypes"
SelectedValue='<%# Eval("STEP_TYPE_ID")%>'
DataTextField="STEP_TYPE"
DataValueField="STEP_TYPE_ID"
runat="server"
Width="100px"
Font-Size="X-Small"
AutoPostBack="true"
OnSelectedIndexChanged = "ddl_StepTypes_SelectedIndexChanged">
</asp:DropDownList>
<label id="lblTest"></label>
<!--<asp:TextBox ID="txtStepType" Text='<%# Eval("SERVER_ID")%>' runat="server"></asp:TextBox>-->
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList DataSourceID="ds_StepTypes"
DataTextField="STEP_TYPE" DataValueField="STEP_TYPE_ID" ID="ddl_StepTypes" runat="server" Width="100px" Font-Size="X-Small"></asp:DropDownList>
<!--<asp:TextBox ID="txtStepType" Text='' runat="server"></asp:TextBox>-->
</FooterTemplate>
</asp:TemplateField>
If any more information is needed, please let me know. Thank you.
The problem is that if you add a control to a template, the GridView creates multiple copies of that control, one for each data item. When the item is chosen, you need a way to determine which Dropdownlist was clicked and which row it belongs to.
The way to resolve this problem is to use an event from the GridView.
The GridView.RowCommand event serves this purpose. Unfortunately initially it is supposed to fire whenever any button is clicked in any template. This process, where a control event in a template is turned into an event in the containing control, is called event bubbling.
May be a workaround described here can help you. Alternatively may be this article could also help (or another one referenced at the very bottom).

VB.net. how to do databind for the all dropdownlist in repeater

I have a dropdownlist in my repeater, the dropdownlist is databound by a datasource. Originally all the dropdownlist will have all the same item option. I try to do if one of the showing dropdownlist is selected one option, then in other dropdownlist this option will disapper.
here is the asp:
<asp:Repeater ID="UnitMatchRepeater" runat="server" DataSourceID="OldUnitsDataSource" >
<ItemTemplate>
<tr>
<td>
<asp:Label ID="OldUnitNumber" runat="server" Text='<%# Eval("Vehicle")%>' value='<%#Container.DataItem("LinkNumber") %>'></asp:Label>
</td>
<td> </td>
<td>
<asp:DropDownList ID="NewUnitsDropDownBox" runat="server" DataSourceID="NewUnitsDataSource" DataTextField="UnitNumber"
DataValueField="LinkNumber" OnDataBound="call_it" AutoPostBack="true" value='<%# Container.DataItem("LinkNumber")%>'></asp:DropDownList>
</td>
<br/>
</tr>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="OldUnitsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:minotaurSQLConnectionString %>">
</asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="NewUnitsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:minotaurSQLConnectionString %>"
SelectCommand="SELECT Minotaur.dbo.Units.LinkNumber AS LinkNumber, RTAFleet.dbo.vehfile.vehicle AS UnitNumber FROM RTAFleet.dbo.vehfile INNER JOIN Minotaur.dbo.Units ON RTAFleet.dbo.vehfile.link_number = Minotaur.dbo.Units.LinkNumber AND Minotaur.dbo.Units.AcquisitionOrderID = #AcquisitionOrderID AND Minotaur.dbo.Units.SnapShotID = 1 AND Minotaur.dbo.Units.OldLinkNumber IS NULL">
<SelectParameters>
<asp:QueryStringParameter Name="AcquisitionOrderID" QueryStringField="orderID"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
here is the code behind:
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim LinkNoArray As String() = Request.QueryString("linkNumber").Split(",")
Dim SqlStr As String = "SELECT Vehicle = CASE WHEN ISNUMERIC(RTAFleet.dbo.vehfile.vehicle) = 1 AND RTAFleet.dbo.vehfile.vehicle IS NOT NULL THEN RTAFleet.dbo.vehfile.vehicle ELSE RTAFleet.dbo.vehfile.veh_xref_num END, RTAFleet.dbo.vehfile.link_number AS LinkNumber FROM RTAFleet.dbo.vehfile INNER JOIN Minotaur.dbo.Units ON RTAFleet.dbo.vehfile.link_number = Minotaur.dbo.Units.LinkNumber AND Minotaur.dbo.Units.SnapShotID = 1 WHERE "
For Each Str As String In LinkNoArray
SqlStr = SqlStr & " Minotaur.dbo.Units.LinkNumber = " & Trim(Str) & " OR"
Next
If Not Page.IsPostBack Then
SqlStr = Left(SqlStr, (SqlStr.Length - 2))
OldUnitsDataSource.SelectCommand = SqlStr
UnitMatchRepeater.DataBind()
End If
End Sub
Private Sub UnitMatchRepeater_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles UnitMatchRepeater.ItemCreated
If Not e.Item.ItemType = ListItemType.Item And Not e.Item.ItemType = ListItemType.AlternatingItem Then
Exit Sub
End If
Dim NewUnitsDDB As DropDownList = e.Item.FindControl("NewUnitsDropDownBox")
AddHandler NewUnitsDDB.SelectedIndexChanged, AddressOf NewUnitsDDB_SelectedIndexChanged
End Sub
Protected Sub NewUnitsDDB_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim linkNo As String = CType(sender, DropDownList).SelectedValue
Dim sqlStr As String = "UPDATE dbo.Units SET OldLinkNumber = #oldLinkNum WHERE LinkNumber = #newLinkNum"
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("minotaurSQLConnectionString").ConnectionString)
Dim cmd As SqlCommand = New SqlCommand(sqlStr, con)
cmd.Parameters.Add("#oldLinkNum", SqlDbType.Int).Value = CType(sender, DropDownList).Attributes("value")
cmd.Parameters.Add("#newLinkNum", SqlDbType.Int).Value = CInt(linkNo)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
dlistdatabinding()
End Sub
Private Sub dlistdatabinding()
For Each rep As RepeaterItem In UnitMatchRepeater.Items
Dim list As DropDownList = rep.FindControl("NewUnitsDropDownBox")
list.DataBind()
Next
End Sub
Please help to find out how to make it works.
thanks
Put your drop down list data binding code in the ItemDataBound event of your repeater. This is the event that fires every time it creates a "row" in your repeater. From there you can find the individual drop down list in the item.
The added benefit to this is that as each item is data bound, you could potentially change the contents of the drop down list based on the data you are working with.

Resources