首页 \ 问答 \ 在C#/ .NET中显示来自数据库(While循环)的内容(Display content from Database (While loop) in C# / .NET)

在C#/ .NET中显示来自数据库(While循环)的内容(Display content from Database (While loop) in C# / .NET)

我对.NET很陌生,不幸的是不得不接管一个前同事的项目。

我的部分代码有一个while循环来循环查询结果...

        while (reader.Read())
        {
            pageTitle.Text = reader["title"].ToString();
        }

我目前正在使用它从我的数据库中的表中检索页面标题。

我想在SiteMaster文件的<title></title>中显示它,但不幸的是现在使用这个方法:

<title><asp:Label ID="pageTitle" runat="server" /></title><title><asp:Label ID="pageTitle" runat="server" /></title>中给我一个span标记。 我想完全删除并只有标题值。

任何人都可以解释做到这一点的最佳方法吗?

感谢您的帮助和耐心。

谢谢


I'm very new to .NET and unfortunately having to take over an ex-colleague's project.

Part of my code has a while loop to loop out query results...

        while (reader.Read())
        {
            pageTitle.Text = reader["title"].ToString();
        }

I am currently using this to retrieve the Page Title from a Table in my Database.

I want to display this in my <title></title> of the SiteMaster file, but unfortunately right now using this method:

<title><asp:Label ID="pageTitle" runat="server" /></title> is giving me a span tag inside the <title>. I want to remove the altogether and just have the Title value.

Can anyone explain the best way to do this?

Appreciate your help and patience with this.

Thank you


原文:https://stackoverflow.com/questions/6033054
更新时间:2023-05-20 17:05

最满意答案

正如您所说的那样,您没有在参数中使用它们(在调用函数时会出现这种情况),您在创建函数时出现的参数中使用它们。 ***运算符在每种情况下都有不同的用途。

定义函数时,它们指定位置参数将放在元组中,并且关键字参数将放在dict中。 是的我只是说了一些论点,但在这种情况下它们适用于参数。

def example(*args, **kwargs):
    print "args: {0}".format(args)
    print "kwargs: {0}".format(kwargs)

example(1, 2, 'a', foo='bar', bar='foo')

运行时,输出:

args: (1, 2, 'a')
kwargs: {'foo': 'bar', 'bar': 'foo'}

当我说我们将它应用于函数定义中的参数时,你是否明白我的意思? 参数是1, 2, 'a', foo='bar', bar='foo' 。 参数是*args, **kwargs

现在这里是一个将它们应用于参数的示例。

def example2(a, b, foo=None, bar=None):
    print "a: {0}, b:{1}, foo:{2}, bar: {3}".format(a, b, foo, bar)

args = (1, 2)
kwargs = {'foo': 'bar', 'bar': 'foo'}
example2(*args, **kwargs)

这输出:

a: 1, b:2, foo:bar, bar: foo

你可以看到,当我们将它们应用于参数时(也就是我们调用函数时), *具有扩展列表或元组以填充函数的位置参数的效果, **具有扩展字典的效果填写函数的关键字参数。 在扩展发生后,您只需要确保总共有足够的参数并且没有太多参数。

在最后一个例子中,参数是*args, **kwargs ,参数是a, b, foo=None, bar=None


As you've stated your question, you aren't using them in the arguments (which occur when you are calling a function), you are using them in the parameters which occur when you are creating a function. The * and ** operators serve different purposes in each of those situations.

When you are defining a function, they specify that positional arguments will be placed in a tuple and that keyword arguments will be placed in a dict. Yes I did just say arguments, but they are applied to paramaters in this case.

def example(*args, **kwargs):
    print "args: {0}".format(args)
    print "kwargs: {0}".format(kwargs)

example(1, 2, 'a', foo='bar', bar='foo')

when run, this outputs:

args: (1, 2, 'a')
kwargs: {'foo': 'bar', 'bar': 'foo'}

Do you see what I mean when I say that we applied it to the paramaters in the function definition? the arguments are 1, 2, 'a', foo='bar', bar='foo'. the paramaters are *args, **kwargs.

Now here's an example applying them to arguments.

def example2(a, b, foo=None, bar=None):
    print "a: {0}, b:{1}, foo:{2}, bar: {3}".format(a, b, foo, bar)

args = (1, 2)
kwargs = {'foo': 'bar', 'bar': 'foo'}
example2(*args, **kwargs)

This outputs:

a: 1, b:2, foo:bar, bar: foo

You can see that when we apply them to arguments (that is when we are calling the function), * has the effect of expanding a list or tuple to fill the positional arguments of a function and ** has the effect of expanding a dictionary to fill the keyword arguments of a function. You just need to make sure that there are enough and not too much arguments in total after the expansions have taken place.

in the last example, the arguments are *args, **kwargs and the parameters are a, b, foo=None, bar=None

相关问答

更多
  • 正如您所说的那样,您没有在参数中使用它们(在调用函数时会出现这种情况),您在创建函数时出现的参数中使用它们。 *和**运算符在每种情况下都有不同的用途。 在定义函数时,它们指定位置参数将放在元组中,并且关键字参数将放在dict中。 是的我只是说了一些论点,但在这种情况下它们适用于参数。 def example(*args, **kwargs): print "args: {0}".format(args) print "kwargs: {0}".format(kwargs) example ...
  • 一个lambda函数(或者更准确地说,一个lambda 表达式 )只是一个你可以在现场定义的函数,就在你需要的地方。 例如, f = lambda x: x * 2 与... 完全一样 def f(x): return x * 2 当我确切地说,我的意思是 - 他们反汇编到相同的字节码。 两者之间的唯一区别在于,第二个示例中定义的名称有一个名称。 Lambda表达式变得有用,因为创建一个不是一个声明,这意味着,正如其他人已经回答的那样,您可以这样做 print iterator(lambda x ...
  • 如果绝对不会让用户运行任意Python代码而担心安全风险 ,那么您可以使用eval来创建可调用对象: volume(eval('lambda x: %s' % args.equation), args.a, args.b) If there are absolutely no concerns about security risks from letting the user run arbitrary Python code, then you can use eval to create a cal ...
  • 如果您考虑将名称标签附加到对象,而不是将对象填充到命名框中,Python会更有意义。 def lala(a): n = [0, 0 , 0] a = n 这是正在发生的事情。 您正在接收参数(在本例中为列表)并为其指定名称a 。 您正在创建一个新列表并将其命名为n 。 您将给出名为n的附加名称的列表a 。 两个名称a和n都是lala函数的本地名称,因此它们在函数结束时“到期”。 您在函数中创建的列表现在没有名称,Python会丢弃它。 换句话说, a不是可以放入新列表的框。 这是您给出的名 ...
  • 你说如果你想从python做到这一点,你会使用inspect模块。 为什么不从C API调用inspect模块? 以下C代码打印了python函数的所有参数。 它也应该是有效的C ++代码。 我不经常使用Python C API,所以请告诉我是否可以改进一些东西。 #include #include int main(int argc, char* argv[]) { wchar_t* const program = Py_DecodeLocale(arg ...
  • import re re.findall(r'\\bibitem\{(.*?)\}', latex_text) # ['item1', 'item2'] import re re.findall(r'\\bibitem\{(.*?)\}', latex_text) # ['item1', 'item2']
  • def override(*override_args, **override_kwargs): def outer(f): def inner(*args, **kwargs): min_args_length = min(len(args), len(override_args)) args = list(args) for i in xrange(min_args_length): ...
  • 我这样做 def rolling (df, prefix='r', window=3, method='method_name'): for name in df.columns: df[prefix + name] = df[name].rolling(window).__getattribute__(method)() return df I do this def rolling (df, prefix='r', window=3, method='method_na ...
  • r“”结构只是告诉Python,字符串中的任何内容都应该被解释为原始数据。 "qw\n" == 'qw\n' r"qw\n" == 'qw\\n'. 使用它是因为“\”路径分隔符也用于换行等。 你可以在参与辩论时跳过它; save_geometry(filefullpath)应该做你期望的。 The r"" construct just tells Python that whatever's in the string should be interpreted as raw data. "qw\n" ...
  • 是的,你只需编辑两个语法错误就可以了: 资本'T' True 函数定义def sequence(string, n)后的冒号( def sequence(string, n) 。 更新后的代码如下所示 def perrin(a): if True: #do something here def sequence(string,n): if string == "name": perrin(n) Yes it' ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)