'how to use clientscript in module
I want to use clientscript coding in module, then call it from a page. dose anybody know about it?
module A
Module A
public class sample
Public Shared page As New Page
Public Shared ClientScript As ClientScriptManager = page.ClientScript
Public Shared Sub test()
ClientScript.RegisterClientScriptBlock(page.GetType(), "test", "alert('test')", True)
End Sub
end class
End Module
page B
Public Class sanplepage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call A.sample.test()
end sub
end class
Solution 1:[1]
Start from this:
Module:
Module Module1
Public Sub initTest(ByVal callerPage As Page)
Dim scriptBuilder As StringBuilder = New StringBuilder
scriptBuilder.Append("<script type='text/javascript'> ")
scriptBuilder.Append("function clickByModule(){alert('heyyyyyyy coso, ma come si legge il tuo nome???');} ")
scriptBuilder.Append("</script> ")
callerPage.ClientScript.RegisterClientScriptBlock(callerPage.GetType(), "test", scriptBuilder.ToString)
End Sub
End Module
WebForm back-end:
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub WebForm1_Load(sender As Object, e As EventArgs) Handles Me.Load
Module1.initTest(Me)
End Sub
End Class
WebForm front-end:
<form id="form1" runat="server">
<div>
<input type="button" value="Click Me" onclick="clickByModule();" />
</div>
</form>
Solution 2:[2]
Found few problems with your code.
- You should store
current_tripsomewhere and append it totripswhen you cannot put next cow inside - Your while condition has no sense, you should just iterate over cows.
- You should reset
totalsomewhere. - You don't need trip count, it's just len of
trips - You can iterate over dictionary keys/values at once
Code after improvements:
trips = []
current_trip = []
for cow_name, weight in cow_sort.items():
if (weight + total) > limit: # next cow is to heavy
trips.append(current_trip) # add saved trip
current_trip = [] # and clear it
total = 0
current_trip.append(cow_name) # Our new cow starts new trip then
total += weight
trips.append(current_trip) # Add the last trip
return trips
Do not forget to serve the case when the heaviest cow is heavier than limit!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | G3nt_M3caj |
| Solution 2 | kosciej16 |
