[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

JStraub

6/11/2009 4:16:00 AM

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
4 Answers

allenc

6/11/2009 6:20:00 AM

0

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@microsoft.com.

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

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/aa9...
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.




JStraub

6/11/2009 9:46:00 AM

0

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:

> 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@microsoft.com.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#not....
>
> 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/aa9...
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
>
>

allenc

6/12/2009 2:44:00 AM

0

Hi Jeremy,
>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.ProviderName %>"
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

allenc

6/17/2009 8:01:00 AM

0

Hi Jeremy,

>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