HighTechTalks DotNet Forums  

Overriding List Box

ASP.net Building Controls microsoft.public.dotnet.framework.aspnet.buildingcontrols


Discuss Overriding List Box in the ASP.net Building Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
JStraub
 
Posts: n/a

Default Overriding List Box - 06-10-2009 , 11:16 PM






Hi,

I'm trying to make an enhanced version of the standard list box that appends
a prefix to certain items based on another field within the data source.

While I could go through and add each row seperatly to the underlying list
box, it would seem like I should be able to override something associated
with the standard control in order to have it take this action for each item
(ondatabound?). Am I right about this and if so, what routine should I
override?

Thanks,

Jeremy

Reply With Quote
  #2  
Old   
Allen Chen [MSFT]
 
Posts: n/a

Default RE: Overriding List Box - 06-11-2009 , 01:20 AM






Hi Jeremy,

Quote:
I'm trying to make an enhanced version of the standard list box that
appends
a prefix to certain items based on another field within the data source.

While I could go through and add each row seperatly to the underlying list
box, it would seem like I should be able to override something associated
with the standard control in order to have it take this action for each
item
(ondatabound?). Am I right about this and if so, what routine should I
override?

From your description I don't recommend you write a custom ListBox to
achieve your requirement. You can still use the standard ListBox. As to
your requirement, how about regenerating the datasource? You can edit the
original datasource to add prefix to items and bind the updated datasource
to ListBox.

Please let me know if you can use this approach to solve this issue. If you
have additional questions please feel free to ask.


Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Reply With Quote
  #3  
Old   
JStraub
 
Posts: n/a

Default RE: Overriding List Box - 06-11-2009 , 04:46 AM



Hi,

I guess that would be a possibility, however I'm looking at this as a more
generic solution. Its not that I want to make this modification on a one
time basis (though, I guess modifying the data prior to feeding it to the
list box might be a generic solution). However, based on the re usability of
the custom control, I think that is the most desirable.

That being said, must I take the approach of stepping through each data row
and adding it manually to the list box or can one of the data binding methods
of the control be overridden to perform this action?

Thanks,

Jeremy

"Allen Chen [MSFT]" wrote:

Quote:
Hi Jeremy,

I'm trying to make an enhanced version of the standard list box that
appends
a prefix to certain items based on another field within the data source.

While I could go through and add each row seperatly to the underlying list
box, it would seem like I should be able to override something associated
with the standard control in order to have it take this action for each
item
(ondatabound?). Am I right about this and if so, what routine should I
override?


From your description I don't recommend you write a custom ListBox to
achieve your requirement. You can still use the standard ListBox. As to
your requirement, how about regenerating the datasource? You can edit the
original datasource to add prefix to items and bind the updated datasource
to ListBox.

Please let me know if you can use this approach to solve this issue. If you
have additional questions please feel free to ask.


Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.





Reply With Quote
  #4  
Old   
Allen Chen [MSFT]
 
Posts: n/a

Default RE: Overriding List Box - 06-11-2009 , 09:44 PM



Hi Jeremy,
Quote:
That being said, must I take the approach of stepping through each data
row
and adding it manually to the list box or can one of the data binding
methods
of the control be overridden to perform this action?
Thanks for your update. If you need a custom control to do this you can try
the code below. The database I used is Northwind database:

<form id="form1" runat="server">
<div>

<cc1:MyListBox Width="600" Height="400" ID="MyListBox1"
runat="server" AddAppendDataToPrefix="true"
DataSourceID="SqlDataSource1" DataTextField="CompanyName"
DataTextFieldSeparatorString=" |separator_test| "
DataTextAppendField="Address"
DataValueField="CustomerID">
</cc1:MyListBox>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString1 %>"
DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] =
@CustomerID"
InsertCommand="INSERT INTO [Customers] ([CustomerID],
[CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region],
[PostalCode], [Country], [Phone], [Fax]) VALUES (@CustomerID, @CompanyName,
@ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode,
@Country, @Phone, @Fax)"
ProviderName="<%$
ConnectionStrings:NorthwindConnectionString1.Provi derName %>"
SelectCommand="SELECT [CustomerID], [CompanyName],
[ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode],
[Country], [Phone], [Fax] FROM [Customers]"
UpdateCommand="UPDATE [Customers] SET [CompanyName] =
@CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle,
[Address] = @Address, [City] = @City, [Region] = @Region, [PostalCode] =
@PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax WHERE
[CustomerID] = @CustomerID">
<DeleteParameters>
<asp:Parameter Name="CustomerID" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="ContactTitle" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Region" Type="String" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Fax" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="ContactName" Type="String" />
<asp:Parameter Name="ContactTitle" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Region" Type="String" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Fax" Type="String" />
<asp:Parameter Name="CustomerID" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>

</div>
</form>

public class MyListBox : ListBox {
public string DataTextFieldSeparatorString { get; set; }
public bool AddAppendDataToPrefix { get; set; }
public string DataTextAppendField { get; set; }
protected override void PerformDataBinding(IEnumerable dataSource)
{

base.PerformDataBinding(dataSource);
StringBuilder sb = new StringBuilder();
int i = 0;
foreach (object obj in dataSource)
{

if (!string.IsNullOrEmpty(DataTextField) &&
!string.IsNullOrEmpty(DataTextAppendField))
{
string appendString = DataBinder.GetPropertyValue(obj,
DataTextAppendField, DataTextFormatString);

if (AddAppendDataToPrefix)
{
sb.Append(appendString);
if(!string.IsNullOrEmpty(appendString))
sb.Append(DataTextFieldSeparatorString);
sb.Append(DataBinder.GetPropertyValue(obj,
DataTextField, DataTextFormatString));

}
else
{
sb.Append(DataBinder.GetPropertyValue(obj,
DataTextField, DataTextFormatString));
if(!string.IsNullOrEmpty(appendString))
sb.Append(DataTextFieldSeparatorString);
sb.Append(appendString);

}
this.Items[i].Text = sb.ToString();
}
i++;
sb.Remove(0, sb.Length);
}
}

}
Please let me know if it can meet your requirement.

Regards,
Allen Chen
Microsoft Online Support

Reply With Quote
  #5  
Old   
Allen Chen [MSFT]
 
Posts: n/a

Default RE: Overriding List Box - 06-17-2009 , 03:00 AM



Hi Jeremy,

Quote:
That being said, must I take the approach of stepping through each data
row
and adding it manually to the list box or can one of the data binding
methods
of the control be overridden to perform this action?
Have you tested my code? Can it work?

Regards,
Allen Chen
Microsoft Online Support

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.