简体   繁体   English

将参数传递给C#背后的代码

[英]Passing arguments to code behind c#

We have looked and looked all over the internet but we were unable to find a solution. 我们已经在互联网上四处寻找,但找不到解决方案。 We have a linkbutton and we want to pass a parameter to the code behind. 我们有一个linkbutton,我们想将参数传递给后面的代码。 This is the code: 这是代码:

<% foreach (Academia.CourseEN curso in MyCourses) { %>
            <% string id = curso.Id.ToString(); %>
       <div class="col-xs-12 col-sm-5 col-md-5 col-lg-3">
          <div class="panel panel-warning">
            <div class="panel-heading"><%: curso.Title %></div>
            <div class="panel-body"><%: curso.Id %></div>
            <div class="panel-body"><%: curso.Description %></div>
            <div class="panel-body"><%: curso.Price %></div>
            <div class="panel-body"><%: curso.Category %></div>
            <div class="panel-body">
                <div class="col-md-offset-2 col-md-10">

                <asp:LinkButton ID="ENameLinkBtn" runat="server" Text="Button" CommandArgument='<%# Eval("id") %>' OnClick="DeleteCourse_Click" ></asp:LinkButton>

                </div>
            </div>
          </div>
        </div>
       <%} %>

The problem is that eval always returns an empty value, which is not the case as when we display the id it has a value. 问题在于eval总是返回一个空值,而不是当我们显示具有值的id时,情况并非如此。 I post the code behind code as well: 我也将代码发布在代码后面:

LinkButton btn = (LinkButton)(sender);
string yourValue = btn.CommandArgument;
ErrorMessage.Text = yourValue;

I dont know what else to do or what else to try, anything would behighly appreciated. 我不知道该怎么办或要尝试什么,任何事情将不胜感激。 Thanks everyone. 感谢大家。

You can create a javascript function DeleteCourse() 您可以创建一个JavaScript函数DeleteCourse()

function DeleteCourse()
{
   var param = $("#ENameLinkBtn").val();
   $.ajax(
         url:"your web method",
         data: '"param":"' + param + '"'
         ....)
}

Then assign it to your button OnClick="DeleteCourse()" 然后将其分配给您的按钮OnClick="DeleteCourse()"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM