Jag har en DataList där jag även använder mig av en nästlad DataList. Hur gör jag för att när dessa kontrolle skapats fånga värdena (för att t.ex. spara till databas)? Hej, Hämta värden från DataList
ASPX-SIDA
*********
<code>
<asp:DataList id="dlUserLevels" runat="server" Width="100%">
<ItemTemplate>
<asp:Table Runat="server" Width="100%" CellPadding="2" CellSpacing="0">
<asp:TableRow>
<asp:TableCell CssClass="TableDataHeader">Nivå</asp:TableCell>
<asp:TableCell CssClass="TableDataHeader">Förkortning</asp:TableCell>
<asp:TableCell CssClass="TableDataHeader">Bekrivning</asp:TableCell>
<asp:TableCell CssClass="TableDataHeader" HorizontalAlign="center"><img src="/images/gif/action_small.gif"></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell HorizontalAlign="center">
<%# Container.DataItem("UserLevel") %>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbUserLevelShortName" Runat="server" CssClass="TextField" Width="30" MaxLength="3" Text='<%# Container.DataItem("UserLevelShortName") %>'/>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="tbUserLevelDescription" Runat="server" CssClass="TextField" Width="150" Text='<%# Container.DataItem("UserLevelDescription") %>'/>
</asp:TableCell>
<asp:TableCell HorizontalAlign="center">
<asp:Button ID="btnShowDetails" Runat="server" CssClass="FormButton" CommandName="ShowDetails" CommandArgument='<%# Container.DataItem("UserLevel") %>' Text="Visa detaljer"/>
<asp:Button ID="btnHideDetails" Runat="server" CssClass="FormButton" CommandName="HideDetails" CommandArgument='<%# Container.DataItem("UserLevel") %>' Text="Dölj detaljer" Visible="False"/>
<asp:Button ID="btnAction" Runat="server" CssClass="FormButton" CommandName="SaveItem" CommandArgument='<%# Container.DataItem("UserLevel") %>' Text="Spara nivå"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:DataList ID="dlUserLevelDetails" Runat="server"
RepeatLayout="Table"
RepeatDirection="Vertical"
Width="100%">
<ItemTemplate>
<asp:Table Runat="server" Width="100%" CellPadding="2" CellSpacing="1" BackColor="#EEEEEE">
<asp:TableRow>
<asp:TableCell CssClass="TableDataHeader">Sida</asp:TableCell>
<asp:TableCell CssClass="TableDataHeader" HorizontalAlign="center">Inga rättigheter</asp:TableCell>
<asp:TableCell CssClass="TableDataHeader" HorizontalAlign="center">Läs-rättigheter</asp:TableCell>
<asp:TableCell CssClass="TableDataHeader" HorizontalAlign="center">Läs- och skriv-rättigheter</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell colspan="4"><strong>Företag</strong></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Företagsinformation</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="CompanyDetails" value="0" <%# CheckValue(0, Container.DataItem("CompanyDetails")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="CompanyDetails" value="1" <%# CheckValue(1, Container.DataItem("CompanyDetails")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="CompanyDetails" value="2" <%# CheckValue(2, Container.DataItem("CompanyDetails")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Organisation</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="CompanyOrganization" value="0" <%# CheckValue(0, Container.DataItem("CompanyOrganization")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="CompanyOrganization" value="1" <%# CheckValue(1, Container.DataItem("CompanyOrganization")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="CompanyOrganization" value="2" <%# CheckValue(2, Container.DataItem("CompanyOrganization")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Växla företag</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="ChangeCompany" value="0" <%# CheckValue(0, Container.DataItem("ChangeCompany")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="ChangeCompany" value="1" <%# CheckValue(1, Container.DataItem("ChangeCompany")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="ChangeCompany" value="2" <%# CheckValue(2, Container.DataItem("ChangeCompany")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Nytt företag</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="AddCompany" value="0" <%# CheckValue(0, Container.DataItem("AddCompany")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="AddCompany" value="1" <%# CheckValue(1, Container.DataItem("AddCompany")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="AddCompany" value="2" <%# CheckValue(2, Container.DataItem("AddCompany")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Relaterade företag</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="RelatedCompanies" value="0" <%# CheckValue(0, Container.DataItem("RelatedCompanies")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="RelatedCompanies" value="1" <%# CheckValue(1, Container.DataItem("RelatedCompanies")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="RelatedCompanies" value="2" <%# CheckValue(2, Container.DataItem("RelatedCompanies")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell colspan="4"><strong>Fastigheter</strong></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Fastighetsuppgifter</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="PropertyDetails" value="0" <%# CheckValue(0, Container.DataItem("PropertyDetails")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="PropertyDetails" value="1" <%# CheckValue(1, Container.DataItem("PropertyDetails")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="PropertyDetails" value="2" <%# CheckValue(2, Container.DataItem("PropertyDetails")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Ny fastighet</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="AddProperty" value="0" <%# CheckValue(0, Container.DataItem("AddProperty")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="AddProperty" value="1" <%# CheckValue(1, Container.DataItem("AddProperty")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="AddProperty" value="2" <%# CheckValue(2, Container.DataItem("AddProperty")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Importera</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="ImportProperties" value="0" <%# CheckValue(0, Container.DataItem("ImportProperties")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="ImportProperties" value="1" <%# CheckValue(1, Container.DataItem("ImportProperties")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="ImportProperties" value="2" <%# CheckValue(2, Container.DataItem("ImportProperties")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell colspan="4"><strong>Användare</strong></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Användaruppgifter</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="UserDetails" value="0" <%# CheckValue(0, Container.DataItem("UserDetails")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="UserDetails" value="1" <%# CheckValue(1, Container.DataItem("UserDetails")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="UserDetails" value="2" <%# CheckValue(2, Container.DataItem("UserDetails")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Ny användare</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="AddUser" value="0" <%# CheckValue(0, Container.DataItem("AddUser")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="AddUser" value="1" <%# CheckValue(1, Container.DataItem("AddUser")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="AddUser" value="2" <%# CheckValue(2, Container.DataItem("AddUser")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell colspan="4"><strong>Skador</strong></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Mott. skaderapport</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="DamageReportReceivers" value="0" <%# CheckValue(0, Container.DataItem("DamageReportReceivers")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="DamageReportReceivers" value="1" <%# CheckValue(1, Container.DataItem("DamageReportReceivers")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="DamageReportReceivers" value="2" <%# CheckValue(2, Container.DataItem("DamageReportReceivers")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Basbelopp</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="BaseAmounts" value="0" <%# CheckValue(0, Container.DataItem("BaseAmounts")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="BaseAmounts" value="1" <%# CheckValue(1, Container.DataItem("BaseAmounts")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="BaseAmounts" value="2" <%# CheckValue(2, Container.DataItem("BaseAmounts")) %>></asp:TableCell>
</asp:TableRow>
<asp:TableRow BackColor="#FFFFFF">
<asp:TableCell>Skadestatistik</asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="DamageStatistics" value="0" <%# CheckValue(0, Container.DataItem("DamageStatistics")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="DamageStatistics" value="1" <%# CheckValue(1, Container.DataItem("DamageStatistics")) %>></asp:TableCell>
<asp:TableCell HorizontalAlign="center"><input type="radio" name="DamageStatistics" value="2" <%# CheckValue(2, Container.DataItem("DamageStatistics")) %>></asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</code>
CODEBEHIND
***********
<code>
#Region " SID-GLOBALA OBJEKT "
Private objCryptonizer As New SkadeBas.ValidatorControls.Cryptonizer()
Private objDbConn As New SqlConnection(objCryptonizer.Decrypt_System(ConfigurationSettings.AppSettings("objDbConn")))
Private objUser As New SkadeBas.Users.User()
#End Region
#Region " SID-GLOBALA VARIABLER "
Private intCompanyID As Integer = HttpContext.Current.Session("intCompanyID")
Private strSQL As String
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not (Page.IsPostBack) Then
dlUserLevels.DataSource = GetUserLevelsDataSet()
dlUserLevels.DataBind()
Bind_Data()
End If
End Sub
Public Function GetUserLevelsDataSet() As DataSet
strSQL = "SELECT pkUserLevel AS UserLevel, UserLevelShortName, UserLevelDescription " & _
"FROM tblUserLevels WHERE fkCompanyID = " & intCompanyID
Dim da As New SqlDataAdapter(strSQL, objDbConn)
Dim ds As New DataSet()
da.Fill(ds, "tblUserLevels")
Return ds
End Function
Public Function GetUserLevelDetailsDataSet(ByVal intID As Integer) As DataSet
strSQL = "SELECT CompanyDetails, CompanyOrganization, ChangeCompany, AddCompany, RelatedCompanies, " & _
"PropertyDetails, AddProperty, ImportProperties, UserDetails, AddUser, DamageReportReceivers, BaseAmounts, DamageStatistics " & _
"FROM tblUserLevels WHERE pkUserLevel = " & intID & " AND fkCompanyID = " & intCompanyID
Dim da As New SqlDataAdapter(strSQL, objDbConn)
Dim ds As New DataSet()
da.Fill(ds, "tblUserLevelDetails")
Return ds
End Function
Private Sub dlUserLevels_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dlUserLevels.SelectedIndexChanged
End Sub
Private Sub dlUserLevels_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlUserLevels.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
CType(e.Item.FindControl("btnShowDetails"), Button).CommandArgument = e.Item.ItemIndex
End If
End Sub
Private Sub dlUserLevels_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlUserLevels.ItemCommand
If e.CommandName = "ShowDetails" Then
If e.Item.ItemIndex = e.CommandArgument And (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim intUserLevel As Integer
intUserLevel = CType(e.CommandArgument, Integer)
Dim objChild As DataList
Dim objDS As DataSet = GetUserLevelDetailsDataSet(intUserLevel + 1)
objChild = e.Item.FindControl("dlUserLevelDetails")
objChild.DataSource = objDS
objChild.DataBind()
objChild.Visible = True
CType(e.Item.FindControl("btnShowDetails"), Button).Visible = False
CType(e.Item.FindControl("btnHideDetails"), Button).Visible = True
End If
ElseIf (e.CommandName = "SaveItem") Then
Else
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim objChild As DataList
objChild = e.Item.FindControl("dlUserLevelDetails")
objChild.DataSource = Nothing
objChild.Visible = False
CType(e.Item.FindControl("btnShowDetails"), Button).Visible = True
CType(e.Item.FindControl("btnHideDetails"), Button).Visible = False
End If
End If
End Sub
#Region " SUB (Bind_Data): Binder data till kontroller "
Public Sub Bind_Data()
strSQL = "SELECT COUNT(*) FROM tblUserLevels WHERE fkCompanyID = " & intCompanyID
Dim objCmd As New SqlCommand(strSQL, objDbConn)
'--> Öppnar koppling mot databas
objDbConn.Open()
'--> Antal nivåer
Dim intCompanyUserLevels As Integer = CType(objCmd.ExecuteScalar, Integer)
'--> Stänger koppling mot databas
objDbConn.Close()
If (intCompanyUserLevels = 0) Then
intCompanyUserLevels = 2
End If
lblCompanyUserLevels.Text = "Antal nivåer: " & intCompanyUserLevels
End Sub
#End Region
Public Function CheckValue(ByVal Value_1, ByVal Value_2) As String
If (Value_1 = Value_2) Then
Return "checked"
End If
End Function
</code>Sv: Hämta värden från DataList
Du kan leta efter din kontroll på sidan ex:
<code>
// Find control on page.
Control myControl1 = FindControl("TextBox2"); <-- TextBox2 = ID på kontrollen du letar efter.
</conde>
Om du får tag i den så kastar du om den till den typ kontrollen egentligen är.
<code>
TextBox myTextBox = (TextBox)myControl1;
</code>
Om du inte får tag i kontrollen måste du hämta ut alla dina kontroller ut DataListen och söka i dem.
JN